Topic: Operators in Java
Class: X
Multiple Choice Questions
1. What will be the output of following program ?
class Opr {
public static void main(String args[]) {
boolean ans=false;
System.out.print( ((ans=true)?"Done":"Fail") );
}
}
a) Done b) Fail c) Error d) DoneFail
2. What will be the output of following program?
class Opr{
public static void main(String args[]) {
int x=5,y;
y= ++x + x++ + --x;
System.out.println(x + "," + y);
}
}
a) 6,15 b) 6,18 c) 6,12 d) None of these
3. What will be the output of following program?
class Opr{
public static void main(String args[]) {
int a=10, b=20, result;
result=(b>=a);
System.out.print(result);
}
}
a) Error b) 1 c) True d) 20
4. What will be the output of following program?
class Opr{
public static void main(String args[]) {
System.out.print( ((true && true) || false) );
}
}
a) Error b) false c) true d) None of these
5. What will be the output of following program?
int a,i=10;
a=a+i;
System.out.print(a);
a) 10 b) Garbage c) Error d) None of these
6. What will be the output of following program?
int a=0;
System.out.print((a==1)? "Hello": "Hi");
a) Hello b) Hi c) Null d) None of these
7. Predict the outcome
int var1 = 5;
int var2 = 6;
System.out.println(var1 + var2 + " = " + var1 + var2);
a) 56 = 56 b) 11 = 11
c) 56 = 11 d) 11 = 56
8. Predict the outcome
int a = 5,b=6,c=7;
System.out.println(a+c+b/2+c*a);
a) 80 b) 50
c) 44 d) 110
9. What will be the value of a if
int a='A';
a) 80 b) 97
c) 65 d) 110
10. Explicit type conversion is required in which of the given expressions
a) int x='m'; b) int x=2.5*78;
c) int x=478+6998 d) Int x=35*22;
11. Observe the following code snippet and choose the correct option.
byte b = 10; // line 1
b = b * 10; // line 2
a) Lines 1 and 2 both execute without any error b) Because of line 2, the code will not compile.
c) Because of line 1, the code will not compile. d) None of the above
12. Which of the following is the correct expression that evaluates to true if the number x is
between 1 and 100 or the number is negative?
a) 1<x<100 || x<0 b) ((x<100 && x>1)) || (x<0)
c) ((x<100 && x>1)) && (x<0) d) (1>x>100) || (x<0)
13. What will be the output?
int a=42;
double b=42.25;
System.out.println((a%10)+" "+(b%10));
a) 42 42.5 b) 2 2.25
c) 4.2 4.225 d) 2 4.225
14. What is the output of this program?
int i = 5;
System.out.print(i++); System.out.print(++i);
System.out.print(i--); System.out.print(--i);
a) 6765 b) 5654
c) 5775 d) 5765
15. What is the output of this program?
int a = 10;
String result = a > 5 ? "Hello 1" : "Hello 2";
System.out.println(result);
a) True b) False c) Hello 1 d) Hello 2
16. Modulus operator, %, can be applied to which of these?
a) Both Integers and floating - point numbers b) Integers
c) Floating - point numbers d) None of these
17. What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
a) Integer b) Floating-point numbers
c) Boolean d) None of these
18. What is/are highest order precedence operator(s) in Java?
a) () b) {} c) Both (a) and (b) d) None of these
19. The && and || operators
a) Compare two boolean values b) Compare two numeric values
c) Combine two boolean values d) Combine two numeric values
20. What will be the output of the program?
int x=20;
String sup = (x < 15) ? "s" : (x < 22)? "t" : "h";
System.out.println(sup);
a) s b) t
c) h d) Error
Answer the following:
1. If a =12, b =11 and a+= a++ – (--a * --b). Find the values of a and b.
2. Give the output: int a=10, b=20; c=a++ * ++b; d=++c%a++*c--/b++;
System.out.println("a: "+a+" b: "+b+" c: "+c+" d: "+d);
3. Give the output of the following.
char ch=’A’;
int x = 32, y = 2;
i) System.out.println(ch+y); ii) System.out.println(ch+x);
iii) System.out.println(++ch); iv) System.out.println((char)(ch+x+y));
4. Find the value of z in the following program segment.
int x = 5, y = 12, z;
z = (x>= y ? x – y : y – x );
5. Give the output of the following: int x=10, y=20;
System.out.print( ((x<y)||(x=5)>10)? y : x);
6. For initial value of a=4, b= -3, c=0, what will be the value of the x in following expressions, if
they took place successively?
x = a+ b++ + --c + ++b -c++;
x += ++b + ++a +- -c+ a;
7. Evaluate the expression given below:
If p=7, q=9, r=11; p * (q++ % 4) * (++r)
8. What will be the output of the following program segments.
int a = 0; System.out.println( ((a>0 && a<20)? ++ a:−−a) );
9. int a =21, b = 4, c=0; then evaluate
(i) c+= a++ + 2*++b- a % b;
(ii) c= (--a > ++b) ? (b * 4):(a>5)? ++a : ++b;
10. What is the value of m after evaluating the following expression:
m - = 9%++n + ++n/2; when int m=10,n=6.