Java Programming Fundamentals

Quiz covering core Java programming concepts including static methods and variables, method overriding, initialization order, collections, and language fundamentals

15 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

A Vector is declared as follows. What happens if the code tried to add 6 th element to this Vector

  1. The element will be successfully added
  2. ArrayIndexOutOfBounds Exception
  3. The Vector allocates space to accommodate up to 17 elements
  4. The element will be successfully deleted
Question 2 Multiple Choice (Single Answer)

Which is the data structure used to store sorted map elements ?

  1. HashSet
  2. Hashmap
  3. Map
  4. TreeMap
Question 3 Multiple Choice (Single Answer)

What is the output of following piece of code ? int x = 2; switch (x) { case 1:System.out.println(”1?); case 2: case 3:System.out.println(”3?); case 4: case 5:System.out.println(”5?); }

  1. No output
  2. 3 and 5
  3. 1, 3 and 5
  4. 3
Question 4 Multiple Choice (Single Answer)

A method is defined in a class as : void processUser(int i) { } If this method is overriden in a sub class,_

  1. the new method should return int
  2. the new method can return any type of values
  3. the argument list of new method should exactly match that of overriden method
  4. the return type of new method should nottch that of overriden method
Question 5 Multiple Choice (Single Answer)

package statictest; public class TestStatic { public static void message() { System.out.println("In message"); } public static void main(String arg[]) { TestStatic a = null; a.message (); } }

  1. Null Pointer exception
  2. In message
  3. Compiler error
  4. No output
Question 6 Multiple Choice (Single Answer)

package staticinit; public class TestStaticInit { { System.out.print("I am in Init "); } TestStaticInit() { System.out.print("Constructor "); } static { System.out.print("I am in Static "); } static { System.out.print("I am in Static1 "); } public static void main(String[] args) { System.out.println("Before calling "); TestStaticInit init = new TestStaticInit(); TestStaticInit init1 = new TestStaticInit(); } }

  1. I am in Static. I am in Static1. Before calling. I am in Init. Constructor. I am in Init. Constructor.
  2. Before calling. I am in Static. I am in Static1. I am in Init. Constructor. I am in Init.Constructor.
  3. Before calling. I am in Static. I am in Static1. Constructor. I am in Init. Constructor. I am in Init.
  4. Before calling. Constructor. I am in Static. I am in Static1. I am in Init. Constructor. I am in Init.
Question 7 Multiple Choice (Single Answer)

package staticinit; public class TestStaticInit { { System.out.print("I am in Init "); } TestStaticInit() { System.out.print("Constructor. "); } static { System.out.print("I am in Static. "); } static { System.out.print("I am in Static1. "); } public static void main(String[] args) { } }

  1. No output
  2. I am in Static. I am in Static1.
  3. Run time exception
  4. Compiler error
Question 8 Multiple Choice (Single Answer)

package statictest; class TestStaticSub { static int p = 100; public static void printP() { System.out.println("P in parent class..." + p); } } public class TestStaticVariable extends TestStaticSub { public static void printP() { p = 200; System.out.println("P in sub class ..." + p); } public static void main(String[] args) { TestStaticSub tsv = new TestStaticVariable(); tsv.printP(); } }

  1. P in sub class ... 200
  2. Compiler error
  3. P in parent class...100
  4. Run time exception
Question 9 Multiple Choice (Single Answer)

package statictest; public class TestStaticVar { static int p =100; static void setP(int p) { this.p = p; System.out.print("p== "+p); } public static void main(String[] args) { setP(500); System.out.print("p=="+p); } }

  1. p== 500 p== 100
  2. Run time exception
  3. Compiler error
  4. p== 500 p== 500
Question 10 True/False

Comments can be nested.

  1. True
  2. False
Question 11 True/False

Even an empty file is a valid source file.

  1. True
  2. False
Question 12 True/False

Octal literals begin with 'O'

  1. True
  2. False
Question 13 True/False

main method can be overloaded

  1. True
  2. False
Question 14 True/False

main method can be final.

  1. True
  2. False
Question 15 True/False

finalize method is used to release system resources.

  1. True
  2. False