Online Test

  • Facebook
  • Google
  • LinkedIn
  • Twitter
Question: 1
//Which of the following option is correct for below example ?

package com.javatutsworld.test;

public class MainApp {
	private MainApp(int w) { // line 3
		System.out.print(w);
	}

	public static MainApp() { // line 6
		System.out.print(10);
	}

	public static void main(String args[]) {
		MainApp obj = new MainApp(50);
	}
}
Won't compile because of line (3), constructor can't be private
10 50
50
Won't compile because of line (6), constructor can't be static

Question: 2
Which two options are true?

1.   A class can extend more than one class.
2.   A class can extend only one class but extend many interfaces.
3.   An interface can extend many interfaces.
4.   A class can extend one class and implement many interfaces.
1 and 2
2 and 4
3 and 4
2 and 3

Question: 3
What is polymorphism in Java?
It is when a single variable is used with several different types of related objects at different places in a program.
It is when a program uses several different types of objects, each with its own variable.
It is when a single parent class has many child classes.
It is when a class has several methods with the same name but different parameter types.

Question: 4
//Which line of code is not correctly create and initialize a static array of int elements?


public class MainApp {
	static final int[] a = { 100,200 };

	static final int[] b;static { b=new int[2]; b[0]=100; b[1]=200; }

	static final int[] c = new int[2]{ 100,200 };

}
static final int[] a = { 100,200 };
static final int[] b;static { b=new int[2]; b[0]=100; b[1]=200; }
static final int[] c = new int[2]{ 100,200 };
None of the above

Question: 5
//Which of the following option is correct for below example ?

package com.javatutsworld.test;

class Base {
	final public void show() {
		System.out.println("Base::show() called");
	}
}

class Derived extends Base {
	public void show() {
		System.out.println("Derived::show() called");
	}
}

class MainApp {
	public static void main(String[] args) {
		Base b = new Derived();
		b.show();
	}
}
Base::show() called
Derived::show() called
Compiler Error
Runtime Error

Question: 6
What will be the output of the program? 

package javatutsworld.com;

class MainApp {

	public static void main(String args[]) {
		for (int i = 0; i < 4; i += 2) {
			System.out.print(i + " ");
		}
		System.out.println(i);
	}

}
0 2
0 2 4
0 1 2 3 4
Compilation fails.

Question: 7
//What will be the print when the command line invocation java MainApp a b c


public class MainApp {

	static public void main(String[] args) {
		for (int x = 1; x < args.length; x++) {

			System.out.print(args[x] + " ");

		}

	}

}
a b
b c
a b c
Compilation fails.

Question: 8
Which of the following is incorrect statement about interface?
Interfaces specifies what class must do but not how it does.
Interfaces are specified public if they are to be accessed by any code in the program.
All variables in interface are implicitly final and static.
All variables are static and methods are public if interface is defined pubic.

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

package com.javatutsworld.test;

class Base {
	int value = 0;

	Base() {
		addValue();
	}

	void addValue() {
		value += 10;
	}

	int getValue() {
		return value;
	}
}

class Derived extends Base {
	Derived() {
		addValue();
	}

	void addValue() {
		value += 20;
	}
}

public class MainApp {
	public static void main(String[] args) {
		Base b = new Derived();
		System.out.println(b.getValue());
	}
}
30
10
40
20

Question: 10
Which of the following is correct way of implementing an interface salary by class manager?
class manager extends salary {}
class manager implements salary {}
class manager imports salary {}
None of the mentioned

      Advertisements