Java Programming Fundamentals
A comprehensive quiz on Java programming language features including syntax, object-oriented programming, exception handling, threading, and advanced language concepts.
Questions
Expressions are evaluated from right to left by default.
- True
- False
When assigning a value to a reference variable, type doesn't matter.
- True
- False
Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM.
- IllegalStateException
- NumberFormatException
- IllegalArgumentException
- ClassCastException
class Hello{} class Hai extends Hello{} class Me{ static int x=7; static String s=null; public static void getWeight(Hello m){ int y=0/x; System.out.print(s+""); } public static void main(String [] args){ Hello [] ha={new Hello(),new Hai()}; for(Object o:ha) getWeight((Hello)o); } } When i run as--- java Me.java
- NoClassDefFoundError
- ClassCastException
- ArthmeticException
- IllegalArgumentException
class Emu { 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); } }
- -ic mc of mf
- Compilation fails.
- -ic mc mf of
- -ic mf of
What is the result? class Circus { 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--; } } }
- 8
- 8 7
- An exception is thrown at runtime.
- Compilation fails.
class Eggs { int doX(Long x, Long y) { return 1; } int doX(long... x) { return 2; } int doX(Integer x, Integer y) { return 3; } int doX(Number n, Number m) { return 4; } public static void main(String[] args) { new Eggs().go(); } void go() { short s = 7; System.out.print(doX(s,s) + " "); System.out.println(doX(7,7)); } } What is the result?
- 3 3
- 4 1
- 4 3
- 2 1
class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.print(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.print(" foo"); } }; t.start(); } } What is the result?
- MyThread foo
- MyThread bar
- foo bar
- foo bar baz
public static synchronized void main(String[] args) throws InterruptedException { Thread t = new Thread(); t.start(); System.out.print("X"); t.wait(10000); System.out.print("Y"); } What is the result of this code?
- It prints X and never exits.
- The code does not compile.
- It prints XY with a 10-second delay between X and Y.
- An exception is thrown at runtime.
Which are methods of the Object class? (Choose all that apply.)
- notify();
- wait(long msecs);
- interrupt();
- synchronized();
Which method names follow the JavaBeans standard? (Choose all that apply.)
- getCust
- isColorado
- addSize
- putDimensions
public abstract class AbstractTest { public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main(String[] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } } What is the result?
- An exception occurs at runtime.
- Compilation fails.
- 57 22
- 45 57
import static java.lang.System.*; class _ { static public void main(String... _A_V) { String $ = ""; for(int x=0; ++x < _A_V.length; ) $ += _A_V[x]; out.println($); } } And the command line: java _ - A .
- -A.
- An exception is thrown at runtime.
- -A
- A
- class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. public static void main(String [] args) { 6. X x1 = new X(); 7. X x2 = new Y(); 8. Y y1 = new Y(); 9. // insert code here 10. } 11. } Which, inserted at line 9, will compile? (Choose all that apply.)
- (Y)x2.do2();
- x2.do2();
- ((Y)x2).do2();
- None of the above statements will compile
- class Dog { } 2. class Beagle extends Dog { } 3. 4. class Kennel { 5. public static void main(String [] arfs) { 6. Beagle b1 = new Beagle(); 7. Dog dog1 = new Dog(); 8. Dog dog2 = b1; 9. // insert code here 10. } 11. } Which, inserted at line 9, will compile? (Choose all that apply.)
- Beagle b2 = (Beagle) dog1;
- Runtime Exception
- Beagle b4 = dog2;
- None of the above statements will compile
class Scoop { static int thrower() throws Exception { return 42; } public static void main(String [] args) { try { int x = thrower(); } catch (Exception e) { x++; } finally { System.out.println("x = " + ++x); } } } What is the result?
- x = 42
- x = 43
- x = 44
- Compilation fails.
class Fizz { int x = 5; public static void main(String[] args) { final Fizz f1 = new Fizz(); Fizz f2 = new Fizz(); Fizz f3 = FizzSwitch(f1,f2); System.out.println((f1 == f3) + " " + (f1.x == f3.x)); } static Fizz FizzSwitch(Fizz x, Fizz y) { final Fizz z = x; z.x = 6; return z; } } What is the result?
- true false
- true true
- false true
- false false
What will be the output of the following program?
- 0
- 1
- Compilation Error
- Runtime Exception
What is the output of the program?
- 0
- 1
- Compilation Error
- Runtime Exception
Which of these statements are true?
- There can be any number of var-arg parameters in a method
- Only one var-arg parameter is allowed and it must be declared as last parameter in method definition.
- Only one var-arg parameter is allowed and it must be declared as first parameter in method definition.
- None of the above