You are on page 1of 11

1

1.Write the corresponding java expression for the following


(i) x2 +y2
(ii) a=b4+c4+bc/b
2.Write the following in scientific notation:-
(i)4.16×1012 (ii) 3.567× 10-6
3.Evaluate The following:-
int p=5,q=4;
(i)int r =++p + q++ + q--;
(ii)r=p+1+q++ + p++/++q;
(iii) r=p/q+--q/q++;
(iv) r=p*q+p+2/q-1+p++;
(v) r=p+6*2/4+(p++*(--p/++p-3));
(vi)q-=q--+q++*--q/4;
4.Write the output:-
int x=2,y=3;
int z=x+y;
double f=Math.pow(x,y);
System.out.println(“sum is”+’a’+’b’);
System.out.println(“sum is”+(’a’+’b’));
System.out.println(’a’+’b’);
System.out.println(“the product of”+x+”*”+y+”=” +z);
System.out.println((x+”^”+y+”->”+f);
System.out.println(“sum is\n”+x+y);
System.out.println(“sum is\t”+(x+y));
5.Evaluate The Following:-
(a) int a=5,i=1
System.out.println(a++);
System.out.println(++a);
for(i=1;i<=5;++i)
System.out.println("i is "+i);
System.out.println(“i is “+i);
(b)
for(int j=2;j<10;j++)
{
if(j%2==0)
{ continue;}
System.out.print(j);
}
c) int k; int sum=0;
for(k=1;k<=12;k+=2)
{
if(k%9==0)
break;
2

sum=sum+k;
}
System.out.println(k);
System.out.println(sum);
}

5 ii).Rewrite using a ternary operator:-


a)if (percent>90) grade=’A’;
else if (percent>70)grade=’B’;
else grade=’C’;
b) Write the output :-( 2)

public void dis()


{ int x=2;
int value=(!(x<=0))?((x>0&&x<10)?(5*x):0) : (2*x);
System.out.println(value);}

6) if (m= =0) System.out.print(“Hello”);


else System.out.print(“Good day”);
7)if(a>b)
return true;
else
return false;

8) Rewrite using switch


if(grade = =’A’)
System.out.println(“Excellent”);
else
if(grade= =’B’)
System.out.println(“Good”);
else
if(grade= =’C’)
System.out.println(“average”);
else
System.out.println(“below average”);

9. Write the output:-


public class pre
{
public void a()
{
int sum=15,ctr=5;
sum=sum+ ctr++;
System.out.println(sum);
3

System.out.println(ctr);
}

10.int x=15,y=10,z=25;
int var=x>y&&x>z?x:y>z?y:z;
System.out.println(var);

11.Write the output:-


char ch=’a’;
System.out.println(+ch);
What is operator overloading?
If the same operator is used to perform different operations on different situations,it
is known as operator overloading.
e.g + operator generally operates on 2 operands and finds the sum
In the above program it has taken a unary form and it promotes char to int type.
Hence the output is 97 which is the Unicode value of a
+operator is also overloaded to concatenate 2 strings

12. Fill in the blanks:-


public void prime(int no)
{
int count=1;
for(int i=2;2;i++)
{
if(no%3==0)
4;
}
if(5)
System.out.println(“prime”);
else
System.out.println(“composite”);}

13) The following program prints the series given below:-


1×2+2×3+3×4+….9×10
The parts of the program marked ?1? , ?2?, ?3? , ?4? , ?5? must be replaced by
statements so that the program works correctly.
public void main()
{
int sum=?1?
for(int i=?2?;i?3?;i?4?)
{
sum=sum+?4?;}
System.out.println(?5?);
4

14.Answer the following:-


a)Given the values of a,b,c, evaluate the following(answer true or false)
(a>=b)||(!(c==b)&&(c<a))
(i)a=10,b=10,c=10
(ii)a=9,b=10,c=2
b)(i)Write the prototype of a static method named calculator that accepts 2 short type
variables and returns an integer value.
(ii) Write the prototype of a method that accepts the hours and minutes and returns the time
in minutes
c)Using the not logical operator of java,write the statement :
if x is greater than y then compute the remainder on dividing x by y and subtract 10 from the
result ,finally display the result.

d)Write the output:-


public void a1()
{
int a=10;
int b=20;
while(++a<--b)
System.out.print(b+" ");
System.out.print(a+" ");
}
e)int x=-10;
boolean b=true;
while(b)
{
if(x<0)
{
x++;continue;}
else
break;
}
System.out.println(x);
15) Given the following array
27 23 3 18 24 1 12 60 47 5
Which sorting algorithm would produce the following result after 3 iterations.
1 3 5 18 24 27 12 60 47 2
3
16 a) System.out.println(Math.floor(-9.7567));
System.out.println(Math.abs(-19.7567));
System.out.println(Math.round(9.7567));
System.out.println(Math.ceil(-9.7567));
5

System.out.println(Math.max(-9.7567,8.12));
System.out.println(Math.sqrt(16));
System.out.println(Math.pow(4,2));
System.out.println(Math.rint(9.7567));

16 b) String s=” Nothing succeeds like success”


(i)System.out.println( s.length());
(ii) System.out.println(s.substring(3));
(iii) System.out.println(s.substring(1,5));
(iv) System.out.println(s.charAt(3));
(v) System.out.println(s.indexOf(‘o’));
(vi) System.out.println(s.indexOf(“like”);
(vii) System.out.println(s.replace(‘e’,’o’));
(viii) System.out.println(s.indexOf(‘y’));
(ix)int j=0;
for(int i=20;i<s.length();i++)
{
if(s.charAt(i)==’s’)
j=j+10;
}
System.out.print(j);
(x)System.out.println(s.startsWith(“no”));
(xi)s.toLowerCase();
System.out.println(s);
s.concat(“success”);
System.out.println(s);

17 a).Illustrate a default constructor for a class when an object of the class is created
and initialize the object data members with known variables.
public class digit
{
private int a,b;
}
b) What is meant by a literal and an identifier? Give an example?

18)Write the output:-


class Employee public class travel extends Employee
{ {
float basicSal; float travelAllow;
float houseRent; public tr( float s, float h, float t)
Employee (float b, float h) {
{ super(s,h);
basicSal=b; travelAllow = t;
6

houseRent=h; }
} void gross ()
float netSal ( ) {
{ float g;
float nSal=basicSal+ houseRent; g= netSal() +travelAllow;
return nSal; System.out.println(g);
} }
} }

1.Write the name of the base class and the derived class
2.What will be the out put if the values of s,h and t are 100,50 and 40 respectively
3.What is the function of super and extends
19 )Write the result of:
x-y+x/(y * Math.pow(x,2)) *2
when x=10, y=2
20 a)) Write the conditional statement that compares string str with string constant “ I
AM PROUD TO BE AN INDIAN “ .
b) Identify the error in the following statement and correct it.
Short number= =2;

21)What will be the output of the following method for:


a) Cos(4)
b) Cos(7)
c) What is the method computing?
int Cos(int x)
{
int a=1, s=1;
while (a<=x)
{

s *=a;
a+=1;
}
return s;
}
22)The following program prints the integer codes (ascii code) of a character. Fill in the
blanks numbered 1-5 so that the program works correctly.
Class Fill
{
_______1__ convert(char c)
{
_____2____ d=c;
System.out.println(d);
}
7

public static void Abc()


{
____3___ obj- ______4__ Fill(); /*create object */
char a=’A’;
_____5__; }} /* Call function convert*/

23)What is an infinite loop?Write a for loop statement that initializes a loop variable i
as 100,decrements it by 1 in every pass and executes the loop body infinite no of
times.
24)Define a method extract to extract m number of characters beginning from the nth
index in the string
25)Write a class that has a method to accept the profit and cost price as parameters and
calculate the profit percent using the formula
Profitpercent=profit/cost price*100
Where profit=selling price-cost price

26)class test
{
int num;
void func()
{
int num;
num=5;
}}
(i)In the above function to which variable is the value 5 assigned ?Is it assigned to local
num or instance variable num?
(ii)Write the line inside func() to assign value 5 to the instance variable num
(int p)
27)Read the following class and answer the questions that follow:-
class Ac
{
float x(int p,int q)
{
return p%q;
}
}
(i)Define an overloaded function in the class that returns the quotient of 2 numbers
(ii)Define the integer data members n1 and n2 in the class.Initialise them with the values in
the default constructor.

28)char chars[]={‘p’,’q’,’r’};
8

String S1=new String (chars);


String S2=new String(S1);
System.out.println(S1);
System.out.println(S2);

29)public void out()


{String s="niraj";
int l=s.length();
for(int i=l;i>=0;i--)
System.out.println(s.substring(0,i));}
30)Identify the errors:-
int a[5]={2,3,4,5};
System.out.println(a[5]);
A[3]=15.3
System.out.println(a[2]+5.5);
A[1]=a+1;

31)Find the errors if any,in the following program:-


# include<iostream.h>;
public int get()
{
res=num1+num2;
System.out.println(+result);
}

32)Identify the errors in the following code:


package first; package second;
class Aclass Class Cclass
{ {
public void main() int x;
{ public void disp()
Cclass obj=new Cclass(); {
Obj.x=5; System.out.println(x+10);
Zclass obj1=new Zclass(); }
Obj1.y=5; }
} public class Zclass
{
} int y;
9

}
33)How many types of variables can be defined in a class?
In the class given below categorise the variables according to their types.
class Student1

{
private int rollno;
String name;
static String month;
void getData(String n,int r, String m)
{ rollno=r;
name=n;
month=m;
String sm=rollno+name;
}}

34)class PayRoll
{
public void m()
{
long hoursworked=40;
double pay=10.0,tax=0.10;
System.out.println(“hours worked “+hoursworked);
System.out.println((hoursworked *pay-tax));
}
}
(i)Modify each variable such that the variables are declared and not initialized.Write 3
assignment statements to assign a value to each variable.
(ii)What happens when one of the initializations is removed?
(iii) What happens when one of the declarations is removed?
iv) Can we access the variables pay and tax in the method disp if disp () is defined in the
same class.
.35)Identify the error and rewrite the correct statement
(a) for(int i=1;i<5;i++);
System.out.println(i+2);
(b)class errors
{
10

int x;
double y;
private void errors( )//default constructor
{
x=2.3;
y=34;
}

36).Identify the error and rewrite the correct statement


(a) for(int i=1;i<5;i++);
System.out.println(i+2);
(b)Write the difference between / and /=.

37)READ THE FOLLOWING PROCESS OF ARITHMETIC CALCULATIONS AND


ANSWER THE QUESTIONS THAT FOLLOW:-

=8x+500 for x<=50


=14x+1000for x between 50 and 100 weekly commission
=50x+1000for x>=100

Write the above situation using if-else-if construct


Write the above situation using nested if
Write the above situation using a conditional operator
Write the above situation using switch construct

38)Identify The Errors And Write The Output: -


(i) class Rect
{
int l,b;
public void Rect()//constructor
{
l=12.3;
b=2;
area=l*b;
System.out.println(area);
11

(ii) public int showdata(int a)


{
a>10:return true?return false
System.out.println(a);
}
39)Write the equivalent for loop for the given while loop
int i=2,j=12;
while(i<=j)
{
System.out.println(i);
System.out.println(j);
i++;
j--;}

You might also like