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.
Questions
If we extends class A in Class B they are sharing which relationship?
- Class A 'is-a' Class B
- Class B 'is-a' Class A
- Class A 'has-a' Class B
- Class B 'has-a' Class A
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
- a-i,b-iii,c-iv,d-ii
- a-ii,b-i,c-iv,d-iii
- a-iii,b-i,c-ii,d-iv
- a-i,b-iv-c-ii,d-iii
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
- a-i,b-iii,c-iv,d-ii
- a-ii,b-i,c-iv,d-iii
- a-iii,b-i,c-ii,d-iv
- a-i,b-iv-c-ii,d-iii
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
- a-i,b-iii,c-iv,d-ii
- a-ii,b-i,c-iv,d-iii
- a-iii,b-i,c-ii,d-iv
- a-i,b-iv-c-ii,d-iii
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?
- OF(11) is: 45
- OF(11) is: 56
- Compilation fails.
- Runtime Exception
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 :
- String doFileWork() { return "b"; }
- String doFileWork() throws IOException ( return "b"; }
- String doFileWork(int x) throws IOException { return "b"; }
- String doFileWork() throws FileNotFoundException { return "b"; }
- String doFileWork() throws NumberFormatException { return "b"; }
- String doFileWork() throws NumberFormatException, FileNotFoundException { return "b"; }
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?
- -f and java.ArrayIndexOutOfBoundsException
- -f
- ArrayIndexOutOfBoundsException
- None of these.
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?
- int y = x;
- int y = 10;
- int y = 11;
- None of the above will allow compilation to succeed.
- int y = 12;
- int y = 13;
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?
- -
- -c
- -c2
- -2c
- -c22b
- Compilation fails.
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.)
- Just L1 is sufficient.
- Just L2 is sufficient.
- Just L3 is sufficient.
- Both L1 and L2 are required.
- Both L1 and L3 are required.
- Both L3 and L2 are required.
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?
- -c
- -X
- -cm
- -cmp
- -cmXp
- Compilation fails.
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?
- 8
- 8 7
- 8 7 6
- Compilation fails.
- An exception is thrown at runtime.
public class Demo { public static void main(String[] args) { int i=0; i++=i++;//1 ++i=++i;//2 System.out.println("i ="+i); } }
- //1 will create an compilation error
- //2 will create an compilation error
- //1 and //2 both will create an compilation error
- //1 will create an runtime error
- //2 will create an runtime error
- //1 and //2 both will create an runtime error
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.)
- for(int y : x) {
- for(x : Int y) {
- int y = 0; for(y : x) {
- for(int y=0, z=0; z<x.length; z++) { y = x[z];
- for(int y=0, int z=0, int z=0; z<x.length; z++) { y = x[z];
- int y = 0; for(int z=0; z<x.length; z++) { y = x[z];
- 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?
- abc
- abcde
- Compilation fails due only to an error on line 7.
- Compilation fails due to errors on multiple lines.
- Compilation fails due only to an error on line 10.
- Compilation fails due only to an error on line 11.
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?
- -ic of
- -mf of
- Compilation failure
- -ic mf of
- -ic mc mf of
- -ic mc of mf
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?
- null
- null null
- A ClassCastException is thrown.
- A NullPointerException is thrown.
- A NoClassDefFoundError is thrown.
- An ArrayIndexOutOfBoundsException is thrown.
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?
- Compilation error.
- Runtime error.
- Hello Hi Hi_Hello Hi
- Hi_Hello
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?
- Hello....
- Hi....
- Compilation error
- Runtime error
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?
- OF(11) is: 45
- OF(11) is: 56
- OF(11) is: 89
- OF(11) is: 111
- Compilation fails.
- Runtime Exception