Java Programming Fundamentals
Quiz covering core Java programming concepts including static methods and variables, method overriding, initialization order, collections, and language fundamentals
Questions
A Vector is declared as follows. What happens if the code tried to add 6 th element to this Vector
- The element will be successfully added
- ArrayIndexOutOfBounds Exception
- The Vector allocates space to accommodate up to 17 elements
- The element will be successfully deleted
Which is the data structure used to store sorted map elements ?
- HashSet
- Hashmap
- Map
- TreeMap
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?); }
- No output
- 3 and 5
- 1, 3 and 5
- 3
A method is defined in a class as : void processUser(int i) { } If this method is overriden in a sub class,_
- the new method should return int
- the new method can return any type of values
- the argument list of new method should exactly match that of overriden method
- the return type of new method should nottch that of overriden method
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 (); } }
- Null Pointer exception
- In message
- Compiler error
- No output
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(); } }
- I am in Static. I am in Static1. Before calling. I am in Init. Constructor. I am in Init. Constructor.
- Before calling. I am in Static. I am in Static1. I am in Init. Constructor. I am in Init.Constructor.
- Before calling. I am in Static. I am in Static1. Constructor. I am in Init. Constructor. I am in Init.
- Before calling. Constructor. I am in Static. I am in Static1. I am in Init. Constructor. I am in Init.
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) { } }
- No output
- I am in Static. I am in Static1.
- Run time exception
- Compiler error
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(); } }
- P in sub class ... 200
- Compiler error
- P in parent class...100
- Run time exception
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); } }
- p== 500 p== 100
- Run time exception
- Compiler error
- p== 500 p== 500
Comments can be nested.
- True
- False
Even an empty file is a valid source file.
- True
- False
Octal literals begin with 'O'
- True
- False
main method can be overloaded
- True
- False
main method can be final.
- True
- False
finalize method is used to release system resources.
- True
- False