Question1: Which statement will empty the contents of a StringBuilder variable named sb?

A. sb.delete(0, sb.length());
B. sb.delete(0, sb.size());
C. sb.deleteAll();
D. sb.removeAll()

Answer: A

Questions2: Given below program: What is the result of below program:

class Vehicle {

	int x;
	Vehicle(){
		this(10); // Line N1
	}
	Vehicle(int x){
		this.x = x;
	}
}
public class Car extends Vehicle {
	int y;
	Car(){
	   super();
	   this(20); // Line N2
	}
	Car(int y){
		this.y=y;
	}
	@Override
	public String toString() {
		return super.x + ":"+this.y;
	}
	
	public static void main(String[] args) {
		Vehicle y = new Car();
		System.out.println(y);
	}
}

A. 10:20
B. 0:20
C. Compilation fails at line n1
D. Compilation fails at line n2

Answer: D :

Explanation: At line N2, It will throw error “Constructor call must be the first statement in a constructor”. Super(),this() can not be placed , only either super, this allowed

Why? because it’s a rule of the language, present in the Java Language Specification: a call to another constructor in the same class (the this(...) part) or to a constructor in the super class (using super(...)) must go in the first line. It’s a way to ensure that the parent’s state is initialized before initializing the current object.

Question3: What is the result of below program?

public class CallerTest {

	public static void main(String[] args) {
		Caller c = new Caller();
		c.start();
		c.init();

	}

}
class Caller {
	private void init() {
		System.out.println("Initialized");
	}
	private void start() {
		init();
		System.out.println("Started");
	}
}

A. An exception is thrown at runtime.
B. InitializedStartedInitialized
C. InitializedStarted
D. Compilation fails.

Answer: D Start, init methods visibility must be either default,protected and public

Question4: Given the below program, what is the result

public class TestCode {
	public static void main(String[] args) {
		TestCode test = new TestCode();
		System.out.println(isExists+"");
		isExists = test.doSomething();
		System.out.println(isExists);
		
	}
	public static boolean  doSomething() {
		return !isExists;
	}
	static boolean isExists = false;
}

A. Compilation fails.
B. false true
C. true false
D. true true
E. false false

Answer: B. As shown in below, The program executes and static methods can be accessed from object too

Question5:

public class TerneryOperation {

	public static void main(String[] args) {
		String stuff = "TV";
		String res = null;
		if(stuff.equals("TV")) {
			res = "Walter";
		}else if(stuff.equals("Movie")) {
			res = "White";
		}else {
			res = "No Result";
		}
	}
}

Which code fragment can replace the if block?

A. stuff.equals(“TV”)? res=”Walter”:stuff.equals(“Movie”)? res=”white”:res=”No Result”;
B. res = stuff.equals(“TV”)? “Walter” else stuff.equals(“Movie”)? “white”: “No Result”;
C. res = stuff.equals(“TV”)? stuff.equals(“Movie”)? “Walter”: “white”: “No Result”;
D. res = stuff.equals(“TV”)? “Walter”:stuff.equals(“Movie”)? “white”: “No Result”;

Answer: D

Question6: What is the result of below program?

public class TestOperator {
	public static void main(String[] args) {
		int x=1;
		int y=0;
		if(x++ > ++y) {
			System.out.println("Hello");
		}else {
			System.out.println("Welcome");
		}
		System.out.println("Log "+x+":"+y);
	}
}

A. Hello Log 1:0
B. Hello Log 2:1
C. Welcome Log 2:1
D. Welcome Log 1:0

Answer: C

Question7: What is the result of below program?

public class TestOperator {
	public static void main(String[] args) {
		int x=1;
		int y=0;
		if(x++ > y++) {
			System.out.println("Hello");
		}else {
			System.out.println("Welcome");
		}
		System.out.println("Log "+x+":"+y);
	}
}

A. Hello Log 1:0
B. Hello Log 2:1
C. Welcome Log 2:1
D. Welcome Log 1:0

Answer: B

Leave a Reply

Your email address will not be published. Required fields are marked *