Online Test

  • Facebook
  • Google
  • LinkedIn
  • Twitter
1 . Question :
          //Which is the correct option of this programs?


package test;

public class MainApp {
	public static void main(String[] srgs) {
		try {
			int x = 0;
			int y = 10 / x;
			System.out.print("A");
		} catch (Exception e) {
			System.out.print("B");
		} catch (ArithmeticException ae) {
			System.out.print("C");
		}
	}
}
          
compilation fails
AB
BC
AC

2 . Question :
          //What will be the correct option for the following program

package com.javatutsworld.test;

class A {
	public void methodOne() {
		System.out.print("Class A methodOne");
	}
}

class B extends A {
	public void methodTwo() {
		System.out.print("Class B methodTwo");
	}
}

class C extends B {
	public void methodTwo() {
		System.out.print("Class C methodTwo");
	}

	public void methodThree() {
		System.out.print("Class C methodThree");
	}
}

public class MainApp {
	public static void main(String args[]) {
		A a = new A();
		C c = new C();
		c.methodTwo();
		a = c;
		a.methodThree();
	}
}

          
Class B methodTwo Class C methodThree
Class C methodTwo Class C methodThree
Compiler Error
Runtime Error

3 . Question :
          Which method/methods cannot be overridden?
          
static
final
only A
both A and B

4 . Question :
          What will be the output of the program? 

package javatutsworld.com;

class MainApp {

	public static void main(String args[]) {
		int x = 1, y = 6;
		while (y--) {
			x++;

		}
		System.out.println("x = " + x + " y = " + y);
	}

}
          
x = 6 y = 0
x = 7 y = 0
x = 6 y = -1
Compilation fails.

5 . Question :
          Which of these statements are false:
          
Abstract Class cannot be instantiated but can be invoked if a main exists.
Abstract methods can be static
Both a and b
None of the above

6 . Question :
          //Which are not valid declarations?
          
int $x;
int 12;
int _23;
int java_tuts_world;

7 . Question :
          In the below code to instantiate MyClass, you would write which of the following inside main method?
public class MyClass{
	public static void main(String args[])   {
	}
}

          
MyClass mc = new MyClass();
MyClass mc = MyClass();
It can't be done. The constructor of MyClass should be defined as public void MyClass(){....}
MyClass mc = new MyClass;

8 . Question :
          //What gets printed when the following code is compiled and run with the command - java MainApp 2

package com.javatutsworld.test;

class MainApp {
	public static void main(String args[]) {
		Integer intObj=Integer.valueOf(args[args.length-1]);
		int i = intObj.intValue();
		if(args.length > 1)
			System.out.println(i);
		if(args.length > 0)
			System.out.println(i - 1);
		else
			System.out.println(i - 2);
		}
}

          
test
test -1
0
1

9 . Question :
          //What will be the correct option for the following program

package com.javatutsworld.test;

public class MainApp {
	public static void main(String[] args) {
		Test t1 = new Test(11);
	}
}

class Test {
	int a = 1;

	Test(int a) {
		System.out.println(a + " " + this.a);
	}
}
          
Compile time error.
Run time error
11 1
11 11

10 . Question :
          //Which is the correct option for the below programs?

package test;

public class MainApp {
	public static void main(String[] args) {
		MainApp mainApp = new MainApp();
		mainApp.print();

	}

	public void print() {
		String str = "abcde";
		for (int i = 0; i < 4; i += 2)
			System.out.print(str.charAt(i));
	}

}
          
ac
acd
abc
cde

      Advertisements