Java Programming Fundamentals and OOP Concepts

Test your knowledge of Java programming including object-oriented principles, exception handling, control flow statements, and core language syntax. Covers inheritance, polymorphism, method overriding, try-catch-finally blocks, loops, and more.

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

If we extends class A in Class B they are sharing which relationship?

  1. Class A 'is-a' Class B
  2. Class B 'is-a' Class A
  3. Class A 'has-a' Class B
  4. Class B 'has-a' Class A
Question 2 Multiple Choice (Single Answer)

Match the mobile development platforms with programming languages a. Android i. Java b. Blackberry ii.C c. iPhone iii. Java (RIM APIs) d. BREW iv. Objective C

  1. a-i,b-iii,c-iv,d-ii
  2. a-ii,b-i,c-iv,d-iii
  3. a-iii,b-i,c-ii,d-iv
  4. a-i,b-iv-c-ii,d-iii
Question 3 Multiple Choice (Single Answer)

Match the mobile development platforms with programming languages a. Android i. Java b. Blackberry ii.C c. iPhone iii. Java (RIM APIs) d. BREW iv. Objective C

  1. a-i,b-iii,c-iv,d-ii
  2. a-ii,b-i,c-iv,d-iii
  3. a-iii,b-i,c-ii,d-iv
  4. a-i,b-iv-c-ii,d-iii
Question 4 Multiple Choice (Single Answer)

Match the mobile development platforms with programming languages a. Android i. Java b. Blackberry ii.C c. iPhone iii. Java (RIM APIs) d. BREW iv. Objective C

  1. a-i,b-iii,c-iv,d-ii
  2. a-ii,b-i,c-iv,d-iii
  3. a-iii,b-i,c-ii,d-iv
  4. a-i,b-iv-c-ii,d-iii
Question 5 Multiple Choice (Single Answer)

Given the following code: public class Demo { public int method(int x) { int r = 1; r += x; if ((x > 4) && (x < 10)) { r += 2 * x; } else (x <= 4) { r += 3 * x; } else { r += 4 * x; } r += 5 * x; return r; } public static void main(String [] args) { Demo ob = new Demo(); System.out.println("OF(11) is: " + ob.method (11)); } } What is the result?

  1. OF(11) is: 45
  2. OF(11) is: 56
  3. Compilation fails.
  4. Runtime Exception
Question 6 Multiple Choice (Multiple Answers)

import java.io.*; class Master { String doFileWork() throws FileNotFoundException { return "a"; } } class Slave extends Master { public static void main(String[] args) { String s = null; try { s = new Slave().doFileStuff(); } catch ( Exception x) { s = "b"; } System.out.println(s); } //insert a codeline } Choose the correct answers :

  1. String doFileWork() { return "b"; }
  2. String doFileWork() throws IOException ( return "b"; }
  3. String doFileWork(int x) throws IOException { return "b"; }
  4. String doFileWork() throws FileNotFoundException { return "b"; }
  5. String doFileWork() throws NumberFormatException { return "b"; }
  6. String doFileWork() throws NumberFormatException, FileNotFoundException { return "b"; }
Question 7 Multiple Choice (Single Answer)

class Demo { public static void main(String[] args) { String s = "- " ; try { doMath(args[0]); s += "t "; // line 6 } finally { System.out.println(s += "f "); } } public static void doMath(String a) { int y = 7 / Integer.parseInt(a); } } At commandline if the input is java Demo What will be output?

  1. -f and java.ArrayIndexOutOfBoundsException
  2. -f
  3. ArrayIndexOutOfBoundsException
  4. None of these.
Question 8 Multiple Choice (Single Answer)

class Demo { public static void main(String [] args) { int x = 0; // insert code here do { } while (x++ < y); System.out.println(x); } } Which, inserted at line 4, produces the output 12?

  1. int y = x;
  2. int y = 10;
  3. int y = 11;
  4. None of the above will allow compilation to succeed.
  5. int y = 12;
  6. int y = 13;
Question 9 Multiple Choice (Single Answer)

class Demo { static String s = "-"; public static void main(String[] args) { new Demo().method1() ; System.out.println(s); } void method1() { try { method2();} catch (Exception e) { s += "c"; } } void method2() throws Exception { method3(); s += "2"; method3(); s += "2b"; } void method3() throws Exception { throw new Exception(); } } What is the result?

  1. -
  2. -c
  3. -c2
  4. -2c
  5. -c22b
  6. Compilation fails.
Question 10 Multiple Choice (Multiple Answers)

class Ping extends Utils { public static void main(String [] args) { Utils u = new Ping(); System.out.print(u.getInt(args[0])); } int getInt(String arg) { return Integer.parseInt(arg); } } class Utils { int getInt(String x) throws Exception { return 7; } } And the following three possible changes: L1. Declare that main() throws an Exception. L2. Declare that Ping.getInt() throws an Exception. L3. Wrap the invocation of getInt() in a try / catch block. Which change(s) allow the code to compile? (Choose all that apply.)

  1. Just L1 is sufficient.
  2. Just L2 is sufficient.
  3. Just L3 is sufficient.
  4. Both L1 and L2 are required.
  5. Both L1 and L3 are required.
  6. Both L3 and L2 are required.
Question 11 Multiple Choice (Single Answer)

class Demo { public static void main(String[] args) { String s = "-"; switch(TimeZone.CST) { case EST: s += "e"; case CST: s += "c"; case MST: s += "m"; default: s += "X"; case PST: s += "p"; } System.out.println(s); } } enum TimeZone {EST, CST, MST, PST } What is the result?

  1. -c
  2. -X
  3. -cm
  4. -cmp
  5. -cmXp
  6. Compilation fails.
Question 12 Multiple Choice (Single Answer)

class Demo { public static void main(String[] args) { int x = 9; int y = 6; for(int z = 0; z < 6; z++, y--) { if(x > 2) x--; label: if(x > 5) { System.out.print(x + " "}; --x; continue label; } x--; } } } What is the result?

  1. 8
  2. 8 7
  3. 8 7 6
  4. Compilation fails.
  5. An exception is thrown at runtime.
Question 13 Multiple Choice (Single Answer)

public class Demo { public static void main(String[] args) { int i=0; i++=i++;//1 ++i=++i;//2 System.out.println("i ="+i); } }

  1. //1 will create an compilation error
  2. //2 will create an compilation error
  3. //1 and //2 both will create an compilation error
  4. //1 will create an runtime error
  5. //2 will create an runtime error
  6. //1 and //2 both will create an runtime error
Question 14 Multiple Choice (Multiple Answers)

class Demo { public static void main(String[] args) { int[] x = {7,6,5,4,3,2,1}; // insert code here System.out.print(y + " "); } } } Which, inserted independently at line 4, compiles? (Choose all that apply.)

  1. for(int y : x) {
  2. for(x : Int y) {
  3. int y = 0; for(y : x) {
  4. for(int y=0, z=0; z<x.length; z++) { y = x[z];
  5. for(int y=0, int z=0, int z=0; z<x.length; z++) { y = x[z];
  6. int y = 0; for(int z=0; z<x.length; z++) { y = x[z];
Question 15 Multiple Choice (Single Answer)
  1. class Demo { 2. final static int x2 = 7; 3. final static Integer x4 = 8; 4. public static void main(String[] args) { 5. Integer x1 = 5; 6. String s = "a"; 7. if(x1 < 9) s += "b"; 8. switch(x1) { 9. case 5: s += "c"; 10. case x2: s += "d"; 11. case x4: s += "e"; 12. } 13. System.out.println(s); 14. } 15. } What is the result?
  1. abc
  2. abcde
  3. Compilation fails due only to an error on line 7.
  4. Compilation fails due to errors on multiple lines.
  5. Compilation fails due only to an error on line 10.
  6. Compilation fails due only to an error on line 11.
Question 16 Multiple Choice (Single Answer)

Given:class Demo { static String s = ""; public static void main(String[] args) { try { throw new Exception(); } catch (Exception e) { try { try { throw new Exception(); } catch (Exception ex) { s += "ic"; } throw new Exception(); } catch (Exception x) { s += "mc"; } finally { s += "mf"; } } finally { s +="of"; } System.out.println(s);} }What is the result?

  1. -ic of
  2. -mf of
  3. Compilation failure
  4. -ic mf of
  5. -ic mc mf of
  6. -ic mc of mf
Question 17 Multiple Choice (Single Answer)

Given: class Mineral { } class Gem extends Mineral { } class Miner { static int x = 7; static String s = null; public static void getWeight(Mineral m) { int y = 0 / x; System.out.print(s + " "); } public static void main(String[] args) { Mineral[] ma = {new Mineral(), new Gem()}; for(Object o : ma) getWeight((Mineral) o); } } And the command-line invocation: java Miner.java What is the result?

  1. null
  2. null null
  3. A ClassCastException is thrown.
  4. A NullPointerException is thrown.
  5. A NoClassDefFoundError is thrown.
  6. An ArrayIndexOutOfBoundsException is thrown.
Question 18 Multiple Choice (Single Answer)

class Super { public static int main(int args[]) { System.out.println("Hi"); return 0; } public static void main(String[] args) { System.out.println("Hello"); int[] a={1,2,3,4}; int b=main(a); } } class main extends Super { public static void main(String[] args) { Super ob=new Super(); String[] b={"a","b","c","d"}; ob.main(b); System.out.println("Hi_Hello"); int[] a={1,2,3,4}; int c=main(a); } } what is the result?

  1. Compilation error.
  2. Runtime error.
  3. Hello Hi Hi_Hello Hi
  4. Hi_Hello
Question 19 Multiple Choice (Single Answer)

public class Demo { public static void main(String[] args) { String s1="Hello"; String s2="Hello"; if(s1==s2) System.out.println("Hello...."); else System.out.println("Hi...."); } } what is the result?

  1. Hello....
  2. Hi....
  3. Compilation error
  4. Runtime error
Question 20 Multiple Choice (Single Answer)

Given the following code: public class Demo { public int method(int x) { int r = 1; r += x; if ((x > 4) && (x < 10)) { r += 2 * x; } else (x <= 4) { r += 3 * x; } else { r += 4 * x; } r += 5 * x; return r; } public static void main(String [] args) { Demo ob = new Demo(); System.out.println("OF(11) is: " + ob.method (11)); } } What is the result?

  1. OF(11) is: 45
  2. OF(11) is: 56
  3. OF(11) is: 89
  4. OF(11) is: 111
  5. Compilation fails.
  6. Runtime Exception