About  | Contact  | Site Map  |  Mirror   
Voodoo Exam  |  Question Bank  |  Databases  |  Tutorials   

Exam 3                     Voodoo Exam  |  Question Bank

We have moved to a new website(www.irixtech.com) where you can now take mock exams online, come back & review your answers later, ask questions in our forums & SCJP questions ranging from SCJP 1.4 to SCJP 6.0, including these question banks & dumps from this site.

Click here to be taken to our new website.


This is the HTML version of Exam_3 as included in Voodoo Exam . Answers are available at the end of this page.

The exam is also available as a database for Voodoo Exam . Click here & download the file called Exam_3.zip & add it to your Voodoo Exam . To learn how to add this file to Voodoo Exam see the ReadMe file that comes with the Voodoo Exam download .

If you'd like to see your question's here download Voodoo Exam & add your own questions to it . Then send me the databases you'd have made & along with including your questions in my application I'll make a seperate page where your questions will be hosted . Sharing your knowledge can prove to be a boon for you as well as for the other folks out there . If you can't be bothered with any downloads but would still like to share your wisdom , then mail me .

Most of the questions presented here are actual working codes that you can cut n' paste into your favourite editor . My personal opinion is that the more you code the sooner you'll get a hang of the concepts . So , don't get bugged by seeing around 10 - 12 lines of code , try to get a hang of visualizing things cause you do get a lot of code based questions in the cert .

If you have any questions regarding the content of this question bank mail me . Your comments & suggestions will be highly appreciated .

This question bank contains questions created by Ashish Hareet . If any of the questions included have wrong answers or any ambiguities , please e-mail the author with the details about the problem . If you find any violations whatsoever please e-mail the author . This file is supposed to be present under this home page - http://www.oocities.org/gnashes30 . If you have found this file at some other location please e-mail the author . You can get the most updated/recent version of this file at - http://www.oocities.org/gnashes30 . Authors e-mail - gnashes30@yahoo.com . Other details available at the end of file .

1. Will the following code compile ? If yes , what is the output ?

class Q019 {
	static Object obj ;
	String get ( ) {
		return null ;
	}

	public static void main ( String args [ ] ) {
		System . out . println ( ( new Q019 ( ) ) . get ( ) == ( Q019 . obj ) ) ;
	}
};
	  
Options :
  1. Compiler error
  2. Run-time error
  3. false
  4. true


2. Consider the following declaration

	  byte b = 127 ;
	  
Is the following valid -
	  b >>= 2 ;
      
Options :
  1. Yes
  2. No
  3. Yes , but requires a cast


3. Given the following class file , how can you get a reference to an object of type Inner ?

class Outer {

	// some valid lines of code

	static class Inner {
		//some more valid lines of code
	}
}
	  
Options :
  1. new Outer ( ) . new Inner ( )
  2. Outer . new Inner ( )
  3. new Outer . Inner ( )
  4. Outer . Inner ( )


4. Consider the code below . By making which of the changes listed below can we make the class compile with no errors at compile - time ?

class A {

	private A ( ) { }

	A ( String str ) { }

};

class B extends A {

	public static void main ( String args [ ] ) {
		System . out . println ( " Hello " ) ;
	}
};
	  
Options :
  1. Make the no - args constructor in class A public
  2. Provide a public constructor in class B that takes String argumen
  3. Provide a no - args constructor in class B which calls the String constructor of class A
  4. Nothing can be done - you cannot extend any class that has even a single private constructor


5. Will the code given below compile ? If run with the following command line - java just - what is the output ?


class Test{

	public static void main ( String args [ ] ) {
		String str = args [ 0 ] ;
		switch ( str . equals ( " just " ) )
		{
		case 1 :
			System . out . println ( " case 1 " ) ;
			break ;
		case 2 :
			System . out . println ( " case 2 " ) ;
			break ;
		default :
			break ;		
		}
	}
};e
	  
Options :
  1. Prints " case 1 " to the console
  2. Prints " case 2 " to the console
  3. Compiler error , switch ( str.equals("just")) : Can't convert boolean to int.
  4. No output generated
  5. Compiler error , case 1 : can't convert boolean to int


6. Consider the following code . what is the result of compiling & executing C.java ?

class A { }

class B extends A { }

class C extends B {
	static void doSomething ( A x , A y ) {System.out.print("AA");}
	static void doSomething ( A x , B y ) {System.out.print("AB");}
	static void doSomething ( B x , A y ) {System.out.print("BA");}
	static void doSomething ( B x , B y ) {System.out.print("BB");}
	static void doSomething ( C x , C y ) { System.out.println("CC");}
	
	public static void main(String[] args) {
		doSomething(null,null);
	}
}
	  
Options :
  1. Prints AA to the console
  2. Prints AB to the console
  3. Prints BA to the console
  4. Prints BB to the console
  5. Prints CC to the console


7. Will the following code compile ? If yes , what is the output ?

class Q013 {
	String str ;

	Q013 ( String str ) {
		System . out . print ( str ) ;

	}

	public static void main ( String ka [ ] ) {
		Q013 obj = new Q013 ( " Hello " ) ;
		System . out . print ( obj . str ) ;
	}
}
	  
Options :
  1. Prints " Hello null " to the console
  2. Compile error " Q012.java:16: Variable i may not have been initialized. "
  3. Prints " Hello Hello " to the console
  4. Prints " null Hello " to the console


8. Consider that the following code compiles fine . What is the output ?

class Q029 {
	public static void main ( String args [ ] ) {
		boolean b [ ] = new boolean[ 2 ] ;
		System . out . println ( b [ 1 ] ) ;
	}
};
	  
Options :
  1. Compile error " Variable b may not have been initialized."
  2. Run-time error " Variable b may not have been initialized."
  3. false
  4. true


9. Will the following code compile ? If yes , what is the output generated at runtime ?

class Q022 {
	public static void main ( String ka [ ] ) {
		while ( false ) {
			System . out . println ( " Great " ) ;
		}
	}
};
	  
Options :
  1. Run's with no output
  2. Compile-time error " Statement not reached."
  3. Prints " Great " to the console
  4. Run-time error " Statement not reached."


10. By changing which line in the following code can you make it compile with no compile - time errors .

class StatTest {

	class Int {
		static int x = 10 ; // ---> line 1
		int y = 10 ; // ---> line 2
	};

	static class Inter {
		static int x = 10 ; // ---> line 3
		int y = 10 ; // ---> line 4
	};
};
	  
Options :
  1. Remove static from line 1
  2. Add static to the declaration on line 2
  3. Remove static from line 3
  4. Add static to the declaration on line 4


11. class Q05.java & Just.java both compile fine . What is the result of executing Q05.java ?

class Just {
	int a = 10 ;

	Just ( ) {
		call ( ) ;
	}

	void call ( ) {
		System . out . print( "a = " + a + " ") ;
	}
};

class Q05 extends Just {
	int b = 16 ;

	Q05 ( ) {
		call ( ) ;
	}

	void call ( ) {
		System.out.print( " b = " + b + " " ) ;
	}
	public static void main ( String args [ ] ) {
		new Q05 ( ) ;
	}
}
	  
Options :
  1. Prints a = 10 b = 16
  2. Prints b = 0 b = 16
  3. Prints a = 0 a = 10
  4. Prints b = 0 a = 0
  5. Stop fooling me ! The code doesn't compile because a & b both are referenced before the instance is created .


12. What is the result of compiling & executing the following code ?

class Q06 {
	
	void Q06 ( ) {
		System . out . println ( " In Q06 " ) ;
	}

	public static void main ( String args [ ] ) {
		new Q06 ( ) ;
		System . out . println ( " Done " ) ;
	}
};
	  
Options :
  1. Compiler error - Constructors cannot have a return value
  2. Compiles fine & prints " In Q06 " followed by " Done " to the console
  3. Compiles fine & prints " In Q06 " to the console
  4. Compiles fine & prints " Done " to the console


13. If the following code were to be present in a method ( in a class file ) what would be the output ?

Byte b1 = new Byte("127");
 if(b1.toString() == b1.toString())
       System.out.println("True");
 else
       System.out.println("False");
	  
Options :
  1. Compilation error, toString() is not avialable for Byte.
  2. Prints "True" to the console
  3. Prints "False" to the console


14. Will the following code compile ? If yes what is the output generated ?

class OuterTest {
	
	String id ;

	OuterTest ( ) {
		this . id = " Default " ;
	}

	OuterTest ( String id ) {
		this . id = id ;
	}

	class InnerTest extends OuterTest {

		void doSomething ( ) {
			System . out . println ( id ) ;
		}
	};

	public static void main ( String args [ ] ) {
		OuterTest outer = new OuterTest ( " STP " ) ;
		InnerTest inner = outer . new InnerTest ( ) ;
		inner . doSomething ( ) ;
	}
};
	  
Options :
  1. Compiler error - An inner class can't extend the enclosing class
  2. Compiler error - No constructor provided for the inner class
  3. Compiles fine & prints " Default " to the console
  4. Compiles fine & prints " STP " to the console


15. Will the following code compile ? If yes what is the output ?

class Q012 {

	Q012 ( int i ) {
		System . out . println ( i ) ;
		this . i = i ;

	}

	public static void main ( String ka [ ] ) {
		Q012 obj = new Q012 ( 10 ) ;
	}
}
	  
Options :
  1. 0
  2. 10
  3. Prints " null " to the console
  4. Compile error " Q012.java:10: No variable i defined in class Q012. "


16. Will the following code compile ? If yes , what is the output generated at runtime ?

class Q006 {
	
	public static void main ( String args [ ] ) {
		boolean flag = false ;
		if ( flag = true )
		{
			System . out . print ( " true " ) ;
		}
		if ( flag == false )
		{
			System . out . print ( " false " ) ;
		}
	}
};
	  
Options :
  1. " true " is printed to the console
  2. " false " is printed to the console
  3. Compile-time error " Q006.java:11: Incompatible type for declaration. Can't convert boolean to java.lang.Boolean. "
  4. " true false " is printed to the console


17. What will happen when you invoke the following method?

void infiniteLoop ( ) {
      byte b = 1 ;
        while ( ++b > 0 ) ;
        System . out . println ( " Welcome to Java " ) ;
}
	  
Options :
  1. The loop never ends ( infinite loop ) .
  2. Prints " Welcome to Java " to the console
  3. Compilation error at line 5. ++ operator should not be used for byte type variables.
  4. Prints nothing but terminates after some time


18. Will the following code compile ? If yes , what is the output ?

class OuterTest {
	
	String id ;

	OuterTest ( ) {
		this . id = " Default " ;
	}

	OuterTest ( String id ) {
		this . id = id ;
	}

	class InnerTest extends OuterTest {

		void doSomething ( ) {
			System . out . println ( OuterTest . this . id ) ;
		}
	};

	public static void main ( String args [ ] ) {
		OuterTest outer = new OuterTest ( " STP " ) ;
		InnerTest inner = outer . new InnerTest ( ) ;
		inner . doSomething ( ) ;
	}
};
	  
Options :
  1. Compiler error - An inner class can't extend the enclosing class
  2. Compiler error - No constructor provided for the inner class
  3. Compiles fine & prints " Default " to the console
  4. Compiles fine & prints " STP " to the console


19. Will the following code compile ? If yes , what is the output generated ?

public class Q10 {
       public static void main ( String [ ] args ) {
          int i = 10 ;
          int j = 10 ;
          boolean b = false ;

          if( b = i == j ) // ---> line 1
            System . out . println ( " True " ) ;
         else
            System . out . println ( " False " ) ;
      }
   }
	  
Options :
  1. Compilation error at line marked 1
  2. Some exception thrown at runtime on line marked 1
  3. Some error generated at runtime on the line marked 1
  4. Prints " True " to the console
  5. Prints " False " to the console


20. What is the output of the following code when compiled & executed ?

class Q01 {
	public static void main ( String args [ ] ) {
		double d = -10 / 0.0 ;
		double e = -11 / 0.0 ;
		if ( d == e )
		{
			System . out . println ( " d = e " ) ;
		}
		else
			System . out . println ( " d != e " ) ;
	}
};
	  
Options :
  1. d != e
  2. d = e
  3. ArithmeticException thrown at runtime
  4. DivisionByZeroException thrown at runtime


21. The following code compiles fine . what is the output generated when the code is executed ?

class Star {
	static {
		System . out . print ( " Static " ) ;
	}

	{
		System . out . print ( " Init " ) ;
	}

	Star ( ) {
		System . out . print ( " Const " ) ;
	}

	public static void main ( String args [ ] ) {
		System . out . print ( " main " ) ;
	}
};
	  
Options :
  1. Static Init Const main
  2. main Static
  3. Static main
  4. main Static Init Const


22. What is the output of the following code ?

class A{
	static int i;

	A(){
	 ++i;
	}

	int get(){
		return ++i;
	}
};

class B extends A{

	private B(){
		i++;
	}

	int get(){
		return ( i + 3);
	}
};

class Q028 extends B{

	public static void main(String ka[]){
		Q028 obj = new Q028();
		A ob = new A();
		ob = (A)obj;
		System.out.println(ob.get());
	}
};
	  
Options :
  1. 2
  2. Compile error " No method matching get ( ) found in class Q026 . "
  3. 5
  4. NullPointerException thrown at run-time .


23. Will the following code compile ? If yes , what is the output ?

class Q015 {
	
	public String get ( int i ) {
		return null ;
	}
	
	public static void main ( String ka [ ] ) {
		Q015 obj = new Q015 ( ) ;
		Object i = obj . get ( 10 ) ;
		System . out . println ( i . toString ( ) ) ;
	}
};
	  
Options :
  1. null
  2. NullPointerException at run-time
  3. Compiler error
  4. 0


24. What is the result of compiling & executing the following class ?

class Q02 {
	public static void main ( String args [ ] ) {
		Double d = new Double ( " -10 / 0.0 " ) ;
		System . out . println ( d ) ;
	}
};
	  
Options :
  1. Prints " Double.NEGATIVE_INFINITY " to the console
  2. Prints " -Infinity " to the console
  3. Throws ArithmeticException at runtime
  4. Throws DivisionByZeroException at runtime
  5. Throws NumberFormatException at runtime


25. Will the following code compile ? If yes what is the output ?

class Q04 {
	public static void main ( String args [ ] ) {
		double d = 10 ;
		double e = 10 ;
		if  ( d . equals ( e ) )
		{
			System . out . print ( " True " ) ;
		}
		else
			System . out . print ( " False " ) ;
	}
};
	  
Options :
  1. Prints True on execution
  2. Prints False on execution
  3. Compiler error - can't invoke a method on a double


26. Will the following code compile ? If yes , what is the output ?

class WhiteZombie {

	public static void main ( String args [ ] ) {
		int i = 2 , j = 3 , k = 5;
		String str = " + " , eq = " = ";
		System . out . println ( i + j + str + k + eq + ( i + j + k ) ) ;
	}
};
	  
Options :
  1. 2 3 + 5 = 5
  2. 5 + 5 = 10
  3. 5 + 5 = 2 3 5
  4. Compiler error


27. What is the output generated by the following code ?

class Q03 {
	public static void main ( String args [ ] ) {
		Double a = new Double ( Double . NaN ) ;
		Double b = new Double ( Double . NaN ) ;
		if ( Double . NaN == Double . NaN )
		{
			System . out . print ( " True " ) ;
		}
		else
			System . out . print ( " False " ) ;
		if ( a . equals ( b ) )
		{
			System . out . print ( " True " ) ;
		}
		else
			System . out . print ( " False " ) ;
	}
}
	  
Options :
  1. True False
  2. False True
  3. True True
  4. False False


28. Which of the following keywords can't be applied to a constructor ? Options :

  1. private
  2. native
  3. abstract
  4. final
  5. synchronized
  6. static
  7. friendly


29. Will the following code compile ? If yes , what is the output generated at runtime ?

class Q024 {

	private String get ( String str ) {
		try
		{
			return str ; 
			throw new Exception ( ) ;
		}
		catch ( Exception e )
		{
			return null ;
		}
	}

	public static void main ( String peace [ ] ) {
		try
		{
			System . out . println ( ( new Q024 ( ) ) . get ( "Peace" ) ) ;
		}
		catch ( Exception e )
		{
			System . out . println ( " Argh ! " ) ;
		}
	}
};
	  
Options :
  1. Compile - time error " Statement not reached "
  2. Prints " Argh ! " to the console
  3. Prints " null " to the console
  4. Prints " Peace " to the console


30. What is the result of compiling & executing InnerMet . java ?

class InnerMet {

  public static void main ( String args [ ] ) {
	int x = 10 ;
	final int y = 10 ;

	class Inner {
		int g = x ;
		int r = y ;

		Inner ( ) {
			System . out . println ( " g = " + g + " & r = " + r ) ;
		}
	};

	new Inner ( ) ;
  }
};
	  
Options :
  1. Compiler error - Inner needs to be static since it is defined inside a static method
  2. Compiler error - Can't define an inner class inside a static method
  3. Compiler error - Can't use a non-final local variable from the enclosing block
  4. Compiles fine & prints " g = 10 & r = 10 " when executed


31. Will the output from the following two code snippets be any different ?

Snippet 1 :
		for ( int i = 0 ; i < 5 ; i++ )
		{
			System . out . println ( i ) ;
		}

Snippet 2 :
		for ( int i = 0 ; i < 5 ; ++i )
		{
			System . out . println ( i ) ;
		}
	  
Options :
  1. Yes
  2. No


32. Will the following code compile ? If yes , what is the output generated at runtime ?

class Q023 {

	private String get ( String str ) {
		try
		{
			throw new Exception ( ) ;
			return str ; 
		}
		catch ( Exception e )
		{
			return null ;
		}
	}

	public static void main ( String peace [ ] ) {
		try
		{
			System . out . println ( ( new Q023 ( ) ) . get ( " Peace " ) ) ;
		}
		catch ( Exception e )
		{
			System . out . println ( " Argh ! " ) ;
		}
	}
};
	  
Options :
  1. Compiler error " Statement not reached "
  2. Prints " Argh ! " to the console
  3. Prints " null " to the console
  4. Prints " Peace " to the console


33. Will the following code compile ? If yes , what is the output & if not what is the compiler error ?

class Test {

	static String raid ( ) {
		try
		{
			throw new Exception ( ) ;
		}
		catch ( Exception e )
		{
			int i = 1 / 0 ;
			return " Error " ;
		}
		finally
		{
			return " Peace " ;
		}
	}

	public static void main ( String args [ ] ) {
		System . out . println ( raid ( ) ) ;
	}
};
	  
Options :
  1. Compiler error , return "Error" : statement not reached
  2. Compiler error , i = 1 / 0 : Exception must be caught or declared in the throws clause
  3. Compiles & prints Error when run
  4. Compiles & prints Peace when run


34. What is the compiler error generated by the following piece of code or what is the output if any ?

class Test {

	public static void main ( String args [ ] ) {
		int i = 1 ;
		if ( i = 2 )
		{
			System . out . println ( " In if " ) ;
		}
		else System . out . println ( " In else " ) ;
	}
};
	  
Options :
  1. Prints " In if " to the console
  2. Prints " In else " to the console
  3. Compiler error , if ( i = 2) : can't convert int to boolean
  4. Compiles but no output is generated


35. Will the following code compile ? If yes , what is the output generated at runtime ?

class A {
	static int i ;

	

	private int get ( ) {
		return 1 ;
	}
};

class B extends A {
	

	private int get ( ) {
		return 2 ;
	}
};

class Q027 extends B {

	public static void main ( String ka [ ] ) {
		Q027 obj = new Q027 ( ) ;
		System . out . println ( obj . get ( ) ) ;
	}
};
	  
Options :
  1. 2
  2. Compile error " No method matching get() found in class Q027."
  3. 5
  4. NullPointerException thrown at run-time .


Answers
  1. d
  2. a
  3. a,c
  4. a,c
  5. c
  6. e
  7. a
  8. c
  9. b
  10. a
  11. b
  12. d
  13. c
  14. c
  15. d
  16. a
  17. b
  18. d
  19. d
  20. b
  21. c
  22. c
  23. b
  24. e
  25. c
  26. b
  27. b
  28. b,c,d,e,f
  29. a
  30. c
  31. b
  32. a
  33. d
  34. c
  35. b
Back to - Voodoo Exam  |  Question Bank


Question bank generated on Sept 10 2002 , 5.58 AM
Generator - Voodoo V2HC alpha
Author - Ashish H.
E - mail - gnashes30@yahoo.com
Home page - http://www.oocities.org/gnashes30

Disclaimer  |  Contact  |  Mirror  |  Downloads
© 2000-2002 Ashish Hareet