1. In the following class definition, which is the first line (if any) that causes a compilation error. Select the one correct answer.
    public class test {
    	public static void main(String args[]) {
    		char c;
    		int i;
    		c = 'A';		// 1
    		i = c;		//2
    		c = i + 1;	//3
    		c++;		//4
    	}
    }
    
    1. The line labeled 1.
    2. The line labeled 2.
    3. The line labeled 3.
    4. The line labeled 4.
    5. All the lines are correct and the program compiles.
  2. Which of these assignments are valid. Select the four correct answers.
    1. short s = 28;
    2. float f = 2.3;
    3. double d = 2.3;
    4. int I = ‘1’;
    5. byte b = 12;
  3. What gets printed when the following program is compiled and run. Select the one correct answer.
    class test {
    	public static void main(String args[]) {
    		int i,j,k,l=0;
    		k = l++;
    		j = ++k;
    		i = j++;
    		System.out.println(i);
    	}
    }
    
    1. 0
    2. 1
    3. 2
    4. 3
  4. Which of these lines will compile? Select the four correct answers.
    1. short s = 20;
    2. byte b = 128;
    3. char c = 32;
    4. double d = 1.4;;
    5. float f = 1.4;
    6. byte e = 0;
  5. The signed right shift operator in Java is –. Select the one correct answer.
    1. <<;
    2. >>
    3. >>>;
    4. None of these.
  6. What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
    public 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);
    	}
    }
    1. 0
    2. 1
    3. 2
    4. 3
  7. What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
    public 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);
    	}
    }
    1. 0
    2. 1
    3. 2
    4. 3
  8. What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
    public 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);
    	}
    }
    1. 0
    2. 1
    3. 2
    4. 3
  9. What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
    public 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);
    	}
    }
    1. 0
    2. 1
    3. 2
    4. 3
  10. Which operator is used to perform bitwise inversion in Java. Select the one correct answer.
    1. ~
    2. !
    3. &
    4. |
    5. ^
  11. What gets printed when the following program is compiled and run. Select the one correct answer.
    public class test {
    	public static void main(String args[]) {
    		byte x = 3;
    		x = (byte)~x;
    		System.out.println(x);
    	}
    }
    
    1. 3
    2. 0
    3. 1
    4. 11
    5. 252
    6. 214
    7. 124
    8. -4
  12. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
    public class test {
    	public static void main(String args[]) {
    		int x,y;
    		x = 3 & 5;
    		y = 3 | 5;
    		System.out.println(x + " " + y);
    	}
    }
    1. 7 1
    2. 3 7
    3. 1 7
    4. 3 1
    5. 1 3
    6. 7 3
    7. 7 5
  13. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
    public class test {
    	public static void main(String args[]) {
    		int x,y;
    		x = 1 & 7;
    		y = 3 ^ 6;
    		System.out.println(x + " " + y);
    	}
    }
    1. 1 3
    2. 3 5
    3. 5 1
    4. 3 6
    5. 1 7
    6. 1 5
  14. Which operator is used to perform bitwise exclusive or.
    1. &
    2. ^
    3. |
    4. !
    5. ~
  15. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
    public 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. 1
    2. 2
    3. 3
    4. 4
  16. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
    public 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. 1
    2. 2
    3. 3
    4. 4
  17. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
    public class test {
    	public static void main(String args[]) {
    		int x, y;
    
    		x = 5 >> 2;
    		y = x >>> 2;
     		System.out.println(y);
    	}
    }
    1. 5
    2. 2
    3. 80
    4. 0
    5. 64
  18. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
    public class test {
    	public static void main(String args[]) {
    		int x;
    
    		x = -3 >> 1;
    		x = x >>> 2;
    		x = x << 1;
     		System.out.println(x);
    	}
    }
    
    1. 1
    2. 0
    3. 7
    4. 5
    5. 23
    6. 2147483646
  19. Which of the following are correct. Select all correct answers.
    1. Java provides two operators to do left shift – << and <<<.
    2. >> is the zero fill right shift operator.
    3. >>> is the signed right shift operator.
    4. For positive numbers, results of operators >> and >>> are same.
  20. What is the result of compiling and running the following program. Select one correct answer.
    public class test {
    	public static void main(String args[]) {
    		int i = -1;
    		i = i >> 1;
     		System.out.println(i);
    	}
    }
    1. 63
    2. -1
    3. 0
    4. 1
    5. 127
    6. 128
    7. 255
  21. What all gets printed when the following gets compiled and run. Select the two correct answers.
    public 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;
    		}
    	}
    }
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
  22. What happens when the following class is compiled and run. Select one correct answer.
    public class test {
    	public static void main(String args[]) {
    		int x = 0, y = 1, z;
    		if(x)
    		    z = 0;
    		else
    		    z = 1;
    
    		if(y)
    		    z = 2;
    		else
    		    z = 3;
    		System.out.println(z);
    	}
    }
    1. The program prints 0
    2. The program prints 1
    3. The program prints 2
    4. The program prints 3
    5. The program does not compile because of problems in the if statement.
  23. Which all lines are part of the output when the following code is compiled and run. Select the nine correct answers.
    public 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);
    		}
    	    }
     	}
    }
    
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3
    17. The program does not print anything.
  24. Which all lines are part of the output when the following code is compiled and run. Select the one correct answer.
    public 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);
    		}
    	    }
     	}
    }
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3
    17. The program does not print anything.
  25. Which all lines are part of the output when the following code is compiled and run. Select the six correct answers.
    public 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);
    		}
    	    }
     	}
    }
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3
  26. Which all lines are part of the output when the following code is compiled and run. Select the six correct answers.
    public 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);
    		}
    	    }
     	}
    }
    
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3
  27. Which all lines are part of the output when the following code is compiled and run. Select the three correct answers.
    public 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);
    		}
    	    }
     	}
    }
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3

Answers to questions on Operators and Assignments

  1. c. It is not possible to assign an integer to a character in this case without a cast.
  2. a, c, d, e. 2.3 is of type double. So it cannot be assigned to a float without a cast.
  3. b
  4. 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.
  5. b
  6. b. In the second assignment to variable b, the expression (i+=2) does not get evaluated.
  7. d
  8. c
  9. d
  10. a
  11. h
  12. c
  13. f
  14. b
  15. a
  16. d
  17. d
  18. f
  19. d
  20. b
  21. a, c
  22. e. The expression in the if statement must evaluate to a boolean.
  23. b, c, d, e, g, h, i, j, l
  24. q
  25. b, c, d, g, h, l
  26. b, c, d, g, h, l
  27. b, c, d