PREV NEXT
Java Operator and Assignments Questions:
- In the following class definition, which is the first line (if any) that causes a compilation error. Select the one correct answer.
12345678910public class test {public static void main(String args[]) {char c;int i;c = 'A'; // 1i = c; //2c = i + 1; //3c++; //4}}- The line labeled 1.
- The line labeled 2.
- The line labeled 3.
- The line labeled 4.
- All the lines are correct and the program compiles.
- Which of these assignments are valid. Select the four correct answers.
- short s = 28;
- float f = 2.3;
- double d = 2.3;
- int I = ‘1’;
- byte b = 12;
- What gets printed when the following program is compiled and run. Select the one correct answer.
123456789class test {public static void main(String args[]) {int i,j,k,l=0;k = l++;j = ++k;i = j++;System.out.println(i);}}- 0
- 1
- 2
- 3
- Which of these lines will compile? Select the four correct answers.
- short s = 20;
- byte b = 128;
- char c = 32;
- double d = 1.4;;
- float f = 1.4;
- byte e = 0;
- The signed right shift operator in Java is –. Select the one correct answer.
- <<;
- >>
- >>>;
- None of these.
- What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
12345678910public class ShortCkt {public static void main(String args[]) {int i = 0;boolean t = true;boolean f = false, b;b = (t && ((i++) == 0));b = (f && ((i+=2) > 0));System.out.println(i);}}- 0
- 1
- 2
- 3
- What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
12345678910public class ShortCkt {public static void main(String args[]) {int i = 0;boolean t = true;boolean f = false, b;b = (t & ((i++) == 0));b = (f & ((i+=2) > 0));System.out.println(i);}}- 0
- 1
- 2
- 3
- What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
12345678910public class ShortCkt {public static void main(String args[]) {int i = 0;boolean t = true;boolean f = false, b;b = (t || ((i++) == 0));b = (f || ((i+=2) > 0));System.out.println(i);}}- 0
- 1
- 2
- 3
- What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
12345678910public class ShortCkt {public static void main(String args[]) {int i = 0;boolean t = true;boolean f = false, b;b = (t | ((i++) == 0));b = (f | ((i+=2) > 0));System.out.println(i);}}- 0
- 1
- 2
- 3
- Which operator is used to perform bitwise inversion in Java. Select the one correct answer.
- ~
- !
- &
- |
- ^
- What gets printed when the following program is compiled and run. Select the one correct answer.
1234567public class test {public static void main(String args[]) {byte x = 3;x = (byte)~x;System.out.println(x);}}- 3
- 0
- 1
- 11
- 252
- 214
- 124
- -4
- What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
12345678public class test {public static void main(String args[]) {int x,y;x = 3 & 5;y = 3 | 5;System.out.println(x + " " + y);}}- 7 1
- 3 7
- 1 7
- 3 1
- 1 3
- 7 3
- 7 5
- What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
12345678public class test {public static void main(String args[]) {int x,y;x = 1 & 7;y = 3 ^ 6;System.out.println(x + " " + y);}}- 1 3
- 3 5
- 5 1
- 3 6
- 1 7
- 1 5
- Which operator is used to perform bitwise exclusive or.
- &
- ^
- |
- !
- ~
- What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
123456789public class test {public static void main(String args[]) {boolean x = true;int a;if(x) a = x ? 1: 2;else a = x ? 3: 4;System.out.println(a);}}- 1
- 2
- 3
- 4
- What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
123456789public class test {public static void main(String args[]) {boolean x = false;int a;if(x) a = x ? 1: 2;else a = x ? 3: 4;System.out.println(a);}}- 1
- 2
- 3
- 4
- What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
123456789public class test {public static void main(String args[]) {int x, y;x = 5 >> 2;y = x >>> 2;System.out.println(y);}}- 5
- 2
- 80
- 0
- 64
- What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
12345678910public class test {public static void main(String args[]) {int x;x = -3 >> 1;x = x >>> 2;x = x << 1;System.out.println(x);}}- 1
- 0
- 7
- 5
- 23
- 2147483646
- Which of the following are correct. Select all correct answers.
- Java provides two operators to do left shift – << and <<<.
- >> is the zero fill right shift operator.
- >>> is the signed right shift operator.
- For positive numbers, results of operators >> and >>> are same.
- What is the result of compiling and running the following program. Select one correct answer.
1234567public class test {public static void main(String args[]) {int i = -1;i = i >> 1;System.out.println(i);}}- 63
- -1
- 0
- 1
- 127
- 128
- 255
- What all gets printed when the following gets compiled and run. Select the two correct answers.
12345678910111213141516public class example {public static void main(String args[]) {int x = 0;if(x > 0) x = 1;switch(x) {case 1: System.out.println(1);case 0: System.out.println(0);case 2: System.out.println(2);break;case 3: System.out.println(3);default: System.out.println(4);break;}}}- 0
- 1
- 2
- 3
- 4
- What happens when the following class is compiled and run. Select one correct answer.
123456789101112131415public class test {public static void main(String args[]) {int x = 0, y = 1, z;if(x)z = 0;elsez = 1;if(y)z = 2;elsez = 3;System.out.println(z);}}- The program prints 0
- The program prints 1
- The program prints 2
- The program prints 3
- The program does not compile because of problems in the if statement.
- Which all lines are part of the output when the following code is compiled and run. Select the nine correct answers.
12345678910public class test {public static void main(String args[]) {for(int i = 0; i < 3; i++) {for(int j = 3; j >= 0; j--) {if(i == j) continue;System.out.println(i + " " + j);}}}}- 0 0
- 0 1
- 0 2
- 0 3
- 1 0
- 1 1
- 1 2
- 1 3
- 2 0
- 2 1
- 2 2
- 2 3
- 3 0
- 3 1
- 3 2
- 3 3
- The program does not print anything.
- Which all lines are part of the output when the following code is compiled and run. Select the one correct answer.
12345678910public class test {public static void main(String args[]) {for(int i = 0; i < 3; i++) {for(int j = 3; j <= 0; j--) {if(i == j) continue;System.out.println(i + " " + j);}}}}- 0 0
- 0 1
- 0 2
- 0 3
- 1 0
- 1 1
- 1 2
- 1 3
- 2 0
- 2 1
- 2 2
- 2 3
- 3 0
- 3 1
- 3 2
- 3 3
- The program does not print anything.
- Which all lines are part of the output when the following code is compiled and run. Select the six correct answers.
12345678910public class test {public static void main(String args[]) {for(int i = 0; i < 3; i++) {for(int j = 3; j >= 0; j--) {if(i == j) break;System.out.println(i + " " + j);}}}}- 0 0
- 0 1
- 0 2
- 0 3
- 1 0
- 1 1
- 1 2
- 1 3
- 2 0
- 2 1
- 2 2
- 2 3
- 3 0
- 3 1
- 3 2
- 3 3
- Which all lines are part of the output when the following code is compiled and run. Select the six correct answers.
12345678910public class test {public static void main(String args[]) {outer: for(int i = 0; i < 3; i++) {for(int j = 3; j >= 0; j--) {if(i == j) continue outer;System.out.println(i + " " + j);}}}}- 0 0
- 0 1
- 0 2
- 0 3
- 1 0
- 1 1
- 1 2
- 1 3
- 2 0
- 2 1
- 2 2
- 2 3
- 3 0
- 3 1
- 3 2
- 3 3
- Which all lines are part of the output when the following code is compiled and run. Select the three correct answers.
12345678910public class test {public static void main(String args[]) {outer : for(int i = 0; i < 3; i++) {for(int j = 3; j >= 0; j--) {if(i == j) break outer;System.out.println(i + " " + j);}}}}- 0 0
- 0 1
- 0 2
- 0 3
- 1 0
- 1 1
- 1 2
- 1 3
- 2 0
- 2 1
- 2 2
- 2 3
- 3 0
- 3 1
- 3 2
- 3 3
Answers to questions on Operators and Assignments
- c. It is not possible to assign an integer to a character in this case without a cast.
- a, c, d, e. 2.3 is of type double. So it cannot be assigned to a float without a cast.
- b
- a, c, d, f. If RHS (Right hand side) is an integer within the correct range of LHS (Left hand side), and if LHS is char, byte, or short, no cast is required. A decimal number is a double by default. Assigning it to float requires a cast.
- b
- b. In the second assignment to variable b, the expression (i+=2) does not get evaluated.
- d
- c
- d
- a
- h
- c
- f
- b
- a
- d
- d
- f
- d
- b
- a, c
- e. The expression in the if statement must evaluate to a boolean.
- b, c, d, e, g, h, i, j, l
- q
- b, c, d, g, h, l
- b, c, d, g, h, l
- b, c, d
Post Your Thoughts