My developer connection home My Developer Connection
knowledgebase for software developers

Questions on Language Fundamentals

 

  1. Which of these are legal identifiers. Select the three correct answers.
    1. number_1
    2. number_a
    3. $1234
    4. -volatile
  2. Which of these are not legal identifiers. Select the four correct answers.
    1. 1alpha
    2. _abcd
    3. xy+abc
    4. transient
    5. account-num
    6. very_long_name
  3. Which of the following are keywords in Java. Select the two correct answers.
    1. friend
    2. NULL
    3. implement
    4. synchronized
    5. throws
  4. Which of the following are Java keywords. Select the four correct answers.
    1. super
    2. strictfp
    3. void
    4. synchronize
    5. instanceof
  5. Which of these are Java keywords. Select the five correct answers
    1. TRUE
    2. volatile
    3. transient
    4. native
    5. interface
    6. then
    7. new
  6. Using up to four characters, write the Java representation of octal literal 6.
  7. Using up to four characters, write the Java representation of integer literal 3 in hexadecimal.
  8. Using up to four characters, write the Java representation of integer literal 10 in hexadecimal.
  9. What is the minimum value of char type. Select the one correct answer.
    1. 0
    2. -215
    3. -28
    4. -215 - 1
    5. -216
    6. -216 - 1
  10. How many bytes are used to represent the primitive data type int in Java. Select the one correct answer.
    1. 2
    2. 4
    3. 8
    4. 1
    5. The number of bytes to represent an int is compiler dependent.
  11. What is the legal range of values for a variable declared as a byte. Select the one correct answer.
    1. 0 to 256
    2. 0 to 255
    3. -128 to 127
    4. -128 to 128
    5. -127 to 128
    6. -215 to 215 - 1
  12. The width in bits of double primitive type in Java is --. Select the one correct answer.
    1. The width of double is platform dependent
    2. 64
    3. 128
    4. 8
    5. 4
  13. What would happen when the following is compiled and executed. Select the one correct answer.
    
    public class Compare { 
       public static void main(String args[]) {
          int x = 10, y;
          if(x < 10) 
             y = 1;
          if(x>= 10) y = 2;
          System.out.println("y is " + y);
       }
    }
    
    
    1. The program compiles and prints y is 0 when executed.
    2. The program compiles and prints y is 1 when executed.
    3. The program compiles and prints y is 2 when executed.
    4. The program does not compile complaining about y not being initialized.
    5. The program throws a runtime exception.
  14. What would happen when the following is compiled and executed. Select the one correct answer.
    
    class example {
       int x;
       int y;
       String name;
       public static void main(String args[]) {
          example pnt = new example();
          System.out.println("pnt is " + pnt.name + 
             " " + pnt.x + " " + pnt.y);		
       }
    }
    
    
    1. The program does not compile because x, y and name are not initialized.
    2. The program throws a runtime exception as x, y, and name are used before initialization.
    3. The program prints pnt is 0 0.
    4. The program prints pnt is null 0 0.
    5. The program prints pnt is NULL false false
  15. The initial value of an instance variable of type String that is not explicitly initialized in the program is --. Select the one correct answer.
    1. null
    2. ""
    3. NULL
    4. 0
    5. The instance variable must be explicitly assigned.
  16. The initial value of a local variable of type String that is not explicitly initialized and which is defined in a member function of a class. Select the one correct answer.
    1. null
    2. ""
    3. NULL
    4. 0
    5. The local variable must be explicitly assigned.
  17. Which of the following are legal Java programs. Select the four correct answers.
    1. // The comments come before the package
      package pkg;
      import java.awt.*;
      class C{}
    2. package pkg;
      import java.awt.*;
      class C{}
    3. package pkg1;
      package pkg2;
      import java.awt.*;
      class C{}
    4. package pkg;
      import java.awt.*;
    5. import java.awt.*;
      class C{}
    6. import java.awt.*;
      package pkg;
      class C {}
  18. Which of the following statements are correct. Select the four correct answers.
    1. A Java program must have a package statement.
    2. A package statement if present must be the first statement of the program (barring any comments).
    3. If a Java program defines both a package and import statement, then the import statement must come before the package statement.
    4. An empty file is a valid source file.
    5. A Java file without any class or interface definitions can also be compiled.
    6. If an import statement is present, it must appear before any class or interface definitions.
  19. What would be the results of compiling and running the following class. Select the one correct answer.
    
    class test {
       public static void main() {
          System.out.println("test");
       }
    }
    
    
    1. The program does not compile as there is no main method defined.
    2. The program compiles and runs generating an output of "test"
    3. The program compiles and runs but does not generate any output.
    4. The program compiles but does not run.
  20. Which of these are valid declarations for the main method? Select the one correct answer.
    1. public void main();
    2. public static void main(String args[]);
    3. static public void main(String);
    4. public static void main(String );
    5. public static int main(String args[]);
  21. Which of the following are valid declarations for the main method. Select the three correct answers.
    1. public static void main(String args[]);
    2. public static void main(String []args);
    3. final static public void main (String args[]);
    4. public static int main(String args[]);
    5. public static abstract void main(String args[]);
  22. What happens when the following program is compiled and executed with the command - java test. Select the one correct answer.
    class test {
       public static void main(String args[]) {
          if(args.length > 0)
             System.out.println(args.length);
       }
    }
    
    
    1. The program compiles and runs but does not print anything.
    2. The program compiles and runs and prints 0
    3. The program compiles and runs and prints 1
    4. The program compiles and runs and prints 2
    5. The program does not compile.
  23. What is the result of compiling and running this program? Select the one correct answer.
    public class test {
       public static void main(String args[]) {
          int i, j;
          int k = 0;
          j = 2;
          k = j = i = 1;
          System.out.println(k);		
       }
    }
    
    
    
    1. The program does not compile as k is being read without being initialized.
    2. The program does not compile because of the statement k = j = i = 1;
    3. The program compiles and runs printing 0.
    4. The program compiles and runs printing 1.
    5. The program compiles and runs printing 2.
  24. What gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the one correct answer.
    public class test {
       public static void main(String args[]) {
          System.out.println(args[0]+" "+args[args.length-1]);
       }
    }
    
    
    1. The program will throw an ArrayIndexOutOfBounds exception.
    2. The program will print "java test"
    3. The program will print "java happens";
    4. The program will print "test happens"
    5. The program will print "lets happens"
  25. What gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the one correct answer.
    public class test {
       public static void main(String args[]) {
          System.out.println(args[0]+" "+args[args.length]);
       }
    }
    
    
    1. The program will throw an ArrayIndexOutOfBounds exception.
    2. The program will print "java test"
    3. The program will print "java happens";
    4. The program will print "test happens"
    5. The program will print "lets happens"
  26. What all gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the two correct answers.
    public class test {
       public static void main(String args[]) {
          System.out.println(args[0]+" "+args.length);
       }
    }
    
    
    1. java
    2. test
    3. lets
    4. 3
    5. 4
    6. 5
    7. 6
  27. What happens when the following program is compiled and run. Select the one correct answer.
    public class example {
       int i = 0;
       public static void main(String args[]) {
          int i = 1;
          i = change_i(i);
          System.out.println(i);
       }
       public static int change_i(int i) {
          i = 2;
          i *= 2;
          return i;
       }
    }
    
    
    1. The program does not compile.
    2. The program prints 0.
    3. The program prints 1.
    4. The program prints 2.
    5. The program prints 4.
  28. What happens when the following program is compiled and run. Select the one correct answer.
    public class example {
       int i = 0;
       public static void main(String args[]) {
          int i = 1;
          change_i(i);
          System.out.println(i);
       }
       public static void change_i(int i) {
          i = 2;
          i *= 2;
       }
    }
    
    
    1. The program does not compile.
    2. The program prints 0.
    3. The program prints 1.
    4. The program prints 2.
    5. The program prints 4.
  29. What happens when the following program is compiled and run. Select the one correct answer.
    public class example {
       int i[] = {0};
       public static void main(String args[]) {
          int i[] = {1};
          change_i(i);
          System.out.println(i[0]);
       }
       public static void change_i(int i[]) {
          i[0] = 2;
          i[0] *= 2;
       }
    }
    
    
    1. The program does not compile.
    2. The program prints 0.
    3. The program prints 1.
    4. The program prints 2.
    5. The program prints 4.
  30. What happens when the following program is compiled and run. Select the one correct answer.
    public class example {
       int i[] = {0};
       public static void main(String args[]) {
          int i[] = {1};
          change_i(i);
          System.out.println(i[0]);
       }
       public static void change_i(int i[]) {
          int j[] = {2};
          i = j;
       }
    }
    
    
    1. The program does not compile.
    2. The program prints 0.
    3. The program prints 1.
    4. The program prints 2.
    5. The program prints 4.

Answers to questions on Language Fundamentals
Java list of keywords:
abstract double int strictfp **
boolean else interface super
break extends long switch
byte final native synchronized
case finally new this
catch float package throw
char for private throws
class goto * protected transient
const * if public try
continue implements return void
default import short volatile
do instanceof static while
  1. a, b, c
  2. a, c, d, e
  3. d, e
  4. a, b, c, e. Please note that strictfp is a new keyword in Java 2. See Sun's site for more details.
  5. b, c, d, e, g
  6. Any of the following are correct answers - 06, 006, or 0006
  7. Any of the following are correct answers - 0x03, 0X03, 0X3 or 0x3
  8. Any of the following are correct answers - 0x0a, 0X0a, 0Xa, 0xa, 0x0A, 0X0A, 0XA, 0xA
  9. a
  10. b
  11. c
  12. b
  13. d. The variable y is getting read before being properly initialized.
  14. d. Instance variable of type int and String are initialized to 0 and null respectively.
  15. a
  16. e
  17. a, b, d, e
  18. b, d, e, f
  19. d
  20. b
  21. a, b, c
  22. a
  23. d
  24. e
  25. a
  26. c, e
  27. e
  28. c
  29. e
  30. c

 

Questions on Operator and Assignments

 

  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

 

Questions on Declaration and Access Control

 

  1. Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.
    1. arr.length
    2. arr.length - 1
    3. arr.size
    4. arr.size - 1
    5. arr.length()
    6. arr.length() - 1
  2. Which of these statements are legal. Select the three correct answers.
    1. int arr[][] = new int[5][5];
    2. int []arr[] = new int[5][5];
    3. int[][] arr = new int[5][5];
    4. int[] arr = new int[5][];
    5. int[] arr = new int[][5];
  3. Write the expression to access the number of elements in a one dimensional array arr. The expression should not be assigned to any variable.
  4. Which of these array declarations and initializations are legal? Select the two correct answers.
    1. int arr[4] = new int[4];
    2. int[4] arr = new int[4];
    3. int arr[] = new int[4];
    4. int arr[] = new int[4][4];
    5. int[] arr = new int[4];
  5. What will the result of compiling and executing the following program. Select the one correct answer.
    class Test {
        public static void main(String args[]) {
    	int arr[] = new int[2];
    	System.out.println(arr[0]);
        }
    }
    
    1. The program does not compile because arr[0] is being read before being initialized.
    2. The program generates a runtime exception because arr[0] is being read before being initialized.
    3. The program compiles and prints 0 when executed.
    4. The program compiles and prints 1 when executed.
    5. The program compiles and runs but the results are not predictable because of un-initialized memory being read.
  6. Which of the following are legal declaration and definition of a method. Select all correct answers.
    1. void method() {};
    2. void method(void) {};
    3. method() {};
    4. method(void) {};
    5. void method {};
  7. Which of the following are valid constructors within a class Test. Select the two correct answers.
    1. test() { }
    2. Test() { }
    3. void Test() { }
    4. private final Test() { }
    5. abstract Test() { }
    6. Test(Test t) { }
    7. Test(void) { }
  8. What is the result of compiling and running the following class. Select the one correct answer.
    class Test {
       public void methodA(int i) {
          System.out.println(i); 
       }
       public int methodA(int i) {
          System.out.println(i+1); 
          return i+1; 
       }
       public static void main(String args[]) {
          Test X = new Test();
          X.methodA(5);
       }
    }
    
    
    Select the one correct answer.
    1. The program compiles and runs printing 5.
    2. The program compiles and runs printing 6.
    3. The program gives runtime exception because it does not find the method Test.methodA(int)
    4. The program give compilation error because methodA is defined twice in class Test.
Answers to questions on Declarations
  1. a
  2. a, b, c
  3. arr.length
  4. c, e. The size of the array should not be specified when declaring the array.
  5. c
  6. a
  7. b, f. A constructor must have the same name as the class, hence a is not a constructor. It must not return any value, hence c is not correct. A constructor cannot be declared abstract or final.
  8. d

 

Questions on Classes

 

  1. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
    protected class example {
    	public static void main(String args[]) {
    		String test = "abc";
    		test = test + test;
    		System.out.println(test);
    	}
    }
    
    
    1. The class does not compile because the top level class cannot be protected.
    2. The program prints "abc"
    3. The program prints "abcabc"
    4. The program does not compile because statement "test = test + test" is illegal.
  2. A top level class may have only the following access modifier. Select the one correct answer.
    1. package
    2. friendly
    3. private
    4. protected
    5. public
  3. Write down the modifier of a method that makes the method available to all classes in the same package and to all the subclasses of this class.
  4. Select the one most appropriate answer. A top level class without any modifier is accessible to -
    1. any class
    2. any class within the same package
    3. any class within the same file
    4. any subclass of this class.
  5. Is this True or False. In Java an abstract class cannot be sub-classed.
  6. Is this True or False. In Java a final class must be sub-classed before it can be used.
  7. Which of the following are true. Select the three correct answers.
    1. A static method may be invoked before even a single instance of the class is constructed.
    2. A static method cannot access non-static methods of the class.
    3. Abstract modifier can appear before a class or a method but not before a variable.
    4. final modifier can appear before a class or a variable but not before a method.
    5. Synchronized modifier may appear before a method or a variable but not before a class.
Answers to questions on classes in Java
  1. a
  2. e
  3. protected
  4. b
  5. False
  6. False
  7. a, b, c. final modifier may appear before a method, a variable or before a class.

 

Questions on Files and Input/Output

This topic is part of SCJP 1.2 exam but not SCJP 1.4 exam.
  1. Which abstract class is the super class of all classes used for reading bytes. Select the one correct answer.
    1. Reader
    2. FileReader
    3. ByteReader
    4. InputStream
    5. FileInputStream
  2. Which abstract class is the super class of all classes used for writing characters. Select the one correct answer.
    1. Writer
    2. FileWriter
    3. CharWriter
    4. OutputStream
    5. FileOutputStream
  3. Which of these are legal ways of accessing a File named "file.tst" for reading. Select the two correct answers.
    1. FileReader fr = new FileReader("file.tst");
    2. FileInputStream fr = new FileInputStream("file.tst");
      InputStreamReader isr = new InputStreamReader(fr, "UTF8");
    3. FileReader fr = new FileReader("file.tst", "UTF8");
    4. InputStreamReader isr = new InputStreamReader("file.tst");
  4. Name the class that allows reading of binary representations of Java primitives from an input byte stream.
  5. Which of these classes are abstract. Select the three correct answers.
    1. FilterWriter
    2. Reader
    3. InputStream
    4. CharArrayReader
    5. DataInputStream
  6. Name the exception thrown by the read method defined in InputStream class
Answers to questions on Files and I/O
  1. d
  2. a
  3. a, b. FileReader class uses the default character encoding, hence c is incorrect. InputStreamReader character class does not have a constructor that takes file name as an argument. Hence d is incorrect.
  4. DataInpiutStream
  5. a,b,c
  6. IOException

 

Questions on Collections

 

  1. TreeMap class is used to implement which collection interface. Select the one correct answer.
    1. Set
    2. SortedSet
    3. List
    4. Tree
    5. SortedMap
  2. Name the Collection interface implemented by the Vector class.
  3. Name the Collection interface implemented by the Hashtable class.
  4. Name the Collection interface implemented by the HashSet class.
  5. Which of these are interfaces in the collection framework. Select the two correct answers.
    1. Set
    2. List
    3. Array
    4. Vector
    5. LinkedList
  6. Which of these are interfaces in the collection framework. Select the two correct answers.
    1. HashMap
    2. ArrayList
    3. Collection
    4. SortedMap
    5. TreeMap
  7. What is the name of collection interface used to maintain non-unique elements in order.
  8. What is the name of collection interface used to maintain unique elements.
  9. What is the name of collection interface used to maintain mappings of keys to values.
  10. Is this true or false. Map interface is derived from the Collection interface.
    1. True
    2. False
Answers to questions on Collections
  1. e
  2. List
  3. Map
  4. Set
  5. a,b
  6. c,d
  7. List
  8. Set
  9. Map
  10. b

 

Questions on Assertions

This topic is part of SCJP 1.4 exam but not SCJP 1.2 exam.

 

  1. What happens when the following code is compiled and run. Select the one correct answer.
    for(int i = 1; i < 3; i++)
      for(int j = 3; j > i; j--)
         assert i!=j {System.out.println(i); }

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  2. What happens when the following code is compiled and run. Select the one correct answer.
    for(int i = 1; i < 3; i++)
      for(int j = 3; j >= 1; j--)
         assert i!=j : i;

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  3. What happens when the following code is compiled and run. Select the one correct answer.
    for(int i = 1; i < 4; i++)
      for(int j = 1; j < 4; j++)
        if(i < j)
           assert i!=j : i;

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  4. Which of the following statement is true about the assert statement. Select the one correct answer.
    1. If a Java class contains assert statements, then it must be compiled with -1.4 option.
    2. When a program having assertions is run, -assertion option must be specified, otherwise the assertions get ignored.
    3. A possible syntax of assert statement is
       assert logical_expression If logical_expression evaluates to true, the program generates an AssertionError.
    4. The program terminates on its first AssertionError.

Answers to questions on Assertions
  1. e. The condition in assert statement must be followed by a semi-colon.
  2. b. When i and j are both 1, assert condition is false, and AssertionError gets generated.
  3. a. When the if condition returns true, the assert statement also returns true. Hence AssertionError does not get generated.
  4. d. The option A is incorrect, as the Java compiler option is -source 1.4 . The option B is incorrect, as the runtime option is -ea or -enableassertions. If the logical expression evaluates to false, then the program generates an AssertionError, hence C is incorrect.

 

Questions on Events

This topic is part of SCJP 1.2 exam but not SCJP 1.4 exam.

 

  1. Name the method defined in EventObject class that returns the Object generated from the event. Select the one correct answer.
    1. getEvent()
    2. getObject()
    3. getID()
    4. getSource()
  2. What is the return type of the method getID() defined in AWTEvent class. Select the one correct answer.
    1. int
    2. long
    3. Object
    4. Component
    5. short
  3. Name the event that gets generated when a button is clicked. Select the one correct answer.
    1. KeyEvent
    2. MouseEvent
    3. ItemEvent
    4. ActionEvent
  4. Which event is generated when the position of a scrollbar is changed. Select the one correct answer.
    1. KeyEvent
    2. MouseEvent
    3. ItemEvent
    4. ActionEvent
    5. AdjustmentEvent
  5. Which of the following Objects can generate ActionEvent. Select the one correct answer.
    1. List
    2. TextArea
    3. CheckboxMenuItem
    4. Choice
  6. Which of the following Objects can generate ItemEvent. Select the two correct answer.
    1. CheckBox
    2. Button
    3. List
    4. MenuItem
  7. Which method identifies the type of an event generated. Select the one correct answer.
    1. getSource()
    2. getType()
    3. getEventType()
    4. getID()
  8. Which of the following are legal adapter classes in Java. Select the two correct answers.
    1. ActionAdapter
    2. ItemAdapter
    3. TextAdapter
    4. MouseAdapter
    5. MouseMotionAdapter
  9. Name the class of the argument of method actionPerformed() defined in the ActionListener interface.
  10. Which of these listener classes have corresponding adapter classes. Select the two correct answers.
    1. ContainerListener
    2. TextListener
    3. ItemListener
    4. MouseMotionListener
  11. Which of these are valid adapter classes. Select the one correct answer.
    1. ActionAdapter
    2. AdjustmentAdapter
    3. KeyAdapter
    4. TextAdapter
  12. Which of these methods are defined in MouseMotionlistener interface. Select the two correct answers.
    1. mouseClicked()
    2. mousePressed()
    3. mouseEntered()
    4. mouseDragged()
    5. mouseMoved()
  13. What is the return type of the method getSource() defined in EventObject class. Select the one correct answer.
    1. int
    2. long
    3. Object
    4. Component
    5. short
Answers to questions on Events
  1. d
  2. a
  3. d
  4. e
  5. a
  6. a, c
  7. d
  8. d, e
  9. ActionEvent
  10. a, d
  11. c
  12. d, e
  13. c

Questions on AWT

 

  1. Which of the following classes are derived from the Container class. Select the four correct answers.
    1. Component
    2. Panel
    3. java.applet.Applet
    4. Dialog
    5. Frame
    6. MenuComponent
  2. Which of the following classes are derived from the Component class. Select the four correct answers.
    1. Container
    2. Window
    3. List
    4. MenuItem
    5. Choice
  3. Name the class used to represent a GUI application window, which is optionally resizable and can have a title bar, an icon, and menus. Select the one correct answer.
    1. Window
    2. Panel
    3. Dialog
    4. Frame
  4. Which abstract class is the super class of all menu related classes.
  5. Which of these classes can be added to a Frame component. Select the three correct answers.
    1. Menu
    2. Button
    3. PopupMenu
    4. Window
    5. List
  6. Which class can be used to represent a checkbox with a textual label that can appear in a menu. Select the one correct answer.
    1. MenuBar
    2. MenuItem
    3. CheckboxMenuItem
    4. Menu
    5. CheckBox
  7. Which of these classes can be added to any Container class, using the add method defined in Container class. Select the two correct answers.
    1. Button
    2. CheckboxMenuItem
    3. Menu
    4. Canvas
Answers to questions on AWT
  1. b, c, d, e
  2. a, b, c, e
  3. d
  4. MenuComponent
  5. b, c, e
  6. c
  7. a, d