(Paper) JAVA Sample Question Bank (Solved)

PAPER : JAVA Sample Question Bank (Solved)

1. Consider the following code , what is the output provided a valid file name is given on the command prompt?

import java.io.*;
	class Q032{

		public static void main(String args[]) throws Exception{
			FileInputStream fin;
			int c = 0;
			try
			{
				fin = new FileInputStream(args[0]);
				while ((c = fin.read()) != -1)
				{
					System.out.print((char)c);
				}
			}
			catch (Exception e)
			{
				System.out.println(e);
			}
			fin.close();
		}
	};

Options :

  1. Compile-time error " Variable fin may not have been initialized."

  2. Run-time exception " java.lang.ArrayIndexOutOfBoundsException: 0"

  3. Run's fine displaying the contents of the file .

  4. Compile-time error " Undefined variable: args"

 

2. What is the output for the following -

class A{
		static int i;

		A(){
		 ++i;
		}

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

	class B extends A{

		B(){
			i++;
		}

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

	class Q028 extends B{

		public static void main(String ka[]){
			Q028 obj = new Q028();
			A ob_a = new A();
			ob_a = (A)obj;
			System.out.println(ob_a.get());
		}
	};

Options :

  1. 2

  2. Compile error " No method matching get() found in class Q026."

  3. 5

  4. NullPointerException thrown at run-time .

 

3. What is the output ?

class Q006{
		
		public static void main(String args[]){
			boolean flag = false;
			if ( flag = true)
			{
				System.out.println("true");
			}
			if ( flag == false)
			{
				System.out.println("false");
			}
		}
	};

Options :

  1. true

  2. false

  3. Compile-time error " Q006.java:11: Incompatible type for declaration. Can't convert boolean to java.lang.Boolean. "

  4. true
    false

 

4. What is the output ?

class A{

		A(){
			System.out.print("1");
		}
	};

	class Q005 extends A{

		Q005(){
			System.out.print("2");
		}

		public static void main(String args[]){
			Q005 obj = new Q005();
			System.out.println("3");
		}
	};

Options :

  1. 123

  2. 23

  3. 3

  4. 1 , 2 , 3 each on a separate line

 

5. What is the output ?

class Q002{

		public static void main(String args[]){
			int i = -1;
			System.out.println(-1 >> 17);
		}
	};

Options :

  1. Compile error

  2. Run-time Exception thrown

  3. -1

  4. Do you expect me to shift the bits

 

6. Predict the output -

class Q008{
		public static void main(String args[]){
			int i = -1;
			System.out.println((i<0)?-i:i);
		}
	};

Options :

  1. 1

  2. -1

  3. Compile error

  4. Run-time error

 

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

class Q009{
		public static void main(String args[]){
			System.out.println((Math.abs(Integer.MIN_VALUE)));
			//FYI Integer.MIN_VALUE = -2147483648
		}
	};

Options :

  1. 2147483648

  2. Compile Error

  3. -2147483648

  4. NegativeArraySizeException thrown at runtime

 

8. 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. null

  4. Compile error " Q012.java:10: No variable i defined in class Q012. "

9. What is the output of this code ?

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

		}

		public static void main(String ka[]){
			Q011 obj = new Q011(10);
			System.out.println(obj.i);
		}
	}

Options :

  1. 10
    10

  2. Compile error " Q012.java:16: Variable i may not have been initialized. "

  3. 0
    10

  4. 10
    0

10. What is the output for this piece ?

class Test{

		static boolean flag;

		public static void main(String args[]){
			if ( flag )
			{
				System.out.println(flag);
			}
		}
	};

Options :

  1. Compiler error , boolean flag : variable flag may not have been initialized

  2. Compiler error , System.out.println ( flag ) : can't convert boolean to String

  3. true

  4. false

  5. Compiles & runs fine with no output generated

 

Answers :

  1. a

  2. b

  3. a

  4. a

  5. c

  6. a

  7. c

  8. d

  9. a

  10. e

 Next

Page 1 of 3

 

comments powered by Disqus