Java Programming Fundamentals
Test your knowledge of Java programming concepts including OOP, servlets, JSP, threading, collections, and exception handling.
Questions
What will be the outcome of line no. 9 in the following program ? 1. public class SearchArray { 2. public static void main(String[] args) { 3. String[] strArr = {"one","two","three","four","five","six","seven"}; 4. boolean[] boolArr = {true,false,false,true,false}; 5. int[] intArr = {22,11,66,99}; 6. Arrays.sort(intArr); 7. for(int n=0; n<intArr.length; n++) 8. {System.out.println(n+". "+intArr[n]);} 9. System.out.println(Arrays.binarySearch(intArr,67)); 10. } 11. }
- -3
- 3
- -4
- 4
Given: 11. public static Iterator reverse(List list) { 12. Collections.reverse(list); 13. return list.iterator(); 14. } 15. public static void main(String[] args) { 16. List list = new ArrayList(); 17. list.add(” 1”); list.add(”2”); list.add(”3”); 18. for (Object obj: reverse(list)) 19. System.out.print(obj + “,”); 20. } ‘What is the result?
- 3,2, 1,
- 1, 2, 3,
- Compilation fails.
- The code runs with no output.
- An exception is thrown at runtime.
Given: 11. public static Collection get() { 12. Collection sorted = new LinkedList(); 13. sorted.add(’B”); sorted.add(”C”); sorted.add(”A”); 14. return sorted; 15. } 16. public static void main(String[] args) { 17. for (Object obj: get()) { 18. System.out.print(obj + “, “); 19. } 20. } What is the result?
- A, B, C,
- B, C, A,
- Compilation fails.
- The code runs with no output.
- An exception is thrown at runtime.
Program output : package com.collection.examples; import java.util.HashSet; public class HashSetExample { public static void main(String[] args) { HashSet s = new HashSet();//1 for(short i = 0; i<100;i++){ //2 s.add(i); //3 s.remove(i-1); //4 } System.out.println(s.size()); //5 } }
- NumberFormat Exception
- 100
- Compilation fails at line 4
- 99
Which of the following options is true for the following program: try { if (choice) { while (true) } else { system .exit(1): } }finally { codetocleanup(); }
- Runtime Exception
- finally block will be executed
- finally block will not be executed
- Compilation error in else block
Is this code legal ? class ExceptionA extends Exception {} class ExceptionB extends ExceptionA {} public class Test{ void thrower() throws ExceptionB{ throw new ExceptionB(); } public static void main(String[] args){ Test t = new Test(); try{t.thrower();} catch(ExceptionA e) {} catch(ExceptionB e) {} } }
- yes
- no
- DO NOT SELECT 1
- DO NOT SELECT 2
Is this code legal ? class ExceptionA extends Exception {} class ExceptionB extends ExceptionA {} class A{ void thrower() throws ExceptionA{ throw new ExceptionA(); } } public class B extends A{ void thrower() throws ExceptionB{ throw new ExceptionB(); } }
- yes
- no
- DO NOT SELECT 1
- DO NOT SELECT 2
What will happen when you attempt to compile and run the following code? public class Bground extends Thread{ public static void main(String argv[]){ Bground b = new Bground(); b.run(); } public void start(){ for (int i = 0; i <10; i++){ System.out.println("Value of i = " + i); } } }
- A compile time error indicating that no run method is defined for the Thread class.
- A run time error indicating that no run method is defined for the Thread class.
- Clean compile and at run time the values 0 to 9 are printed out.
- Clean compile but no output at runtime.
What will be the output of the following program? package scjp.threadTest; public class Ques01 { public static void main(String[] args) { HelloThread hello = new HelloThread(); hello.start(); } } class MyThread implements Runnable{ @Override public void run() { System.out.println("My thread"); } } class HelloThread extends Thread{ public void start(){ System.out.println("Hello thread"); } public void run(){ Thread thread = new Thread(new MyThread()); thread.start(); } }
- The program will output 'Hello Thread' and 'My Thread'.
- The program will output 'Hello Thread'.
- The program will output 'My Thread' and 'Hello Thread'.
- The order of the outputs is not will vary upon execution.
What will be output for the following program? public class BoxingTest { public static void main(String[] args) { byte b = 10; method(b); } static void method(int i){ System.out.println("Primitive Type call"); } static void method(Integer i){ System.out.println("Wrapper Type Call"); } }
- Wrapper Type Call
- Primitive Type Call
- Compiler Error
- Compiles fine, throws runtime exception
How does the set collection deal with duplicate elements?
- An exception is thrown if you attempt to add an element with a duplicate value.
- The add method returns false if you attempt to add an element with a duplicate value.
- A set may contain elements that return duplicate values from a call to the equals method.
- Duplicate values will cause an error at compile time.
Which interface does TreeMap class implement ?
- Set
- SortedSet
- List
- Tree
- SortedMap
A monitor called nik has 3 threads, having same priority, in its waiting pool; How can we notify one of the threads, named 'thread1' to move from Waiting state to Ready State?
- Execute notify(thread1); from within synchronized code of mon.
- Execute mon.notify(thread1); from synchronized code of any object.
- Execute thread1.notify(); from synchronized code of any object.
- Execute thread1.notify(); from any code(synchronized or not) of any object.
- You cannot specify which thread will get notified.
Which statement is true for the following code? public class Rpcraven { public static void main(String argv[]){ Pmcraven pm1 = new Pmcraven("one"); pm1.run(); Pmcraven pm2 = new Pmcraven("two"); pm2.run(); } } class Pmcraven extends Thread { private String sTname=""; Pmcraven(String s) { sTname = s; } public void run(){ for(int i =0; i < 2 ; i++){ try { sleep(1000); }catch(InterruptedException e){ } yield(); System.out.println(sTname); } } }
- Compile time error, class Rpcraven does not import java.lang.Thread
- Output of One One Two Two
- Output of One Two One Two
- Output of One Two Two One
Which of the below is not an implicit object of JSP ?
- request
- session
- context
- page
What are OOPs concepts ?
- Inheritance
- Exception
- deadlock
- Thread
- Networking
Which of the below is not an implicit object of JSP ?
- request
- session
- context
- page
Which of the below is not an implicit object of JSP ?
- request
- session
- context
- page
What are OOPs concepts ?
- Inheritance
- Exception
- deadlock
- Thread
- Networking
GenericServlet is a __________
- Interface
- Class
- Abstract Class
- Filter
- None