public class Demo
{
public ststic void main(String args[])
{
int variable1 = 2;
int variable2 = 3;
for(int counter=1; counter<10 & variable1>1; counter++)
{
if(counter % 2 == 0)
{
System.out.print(counter);
}
else
{
variable1++;
System.out.print(variable1);
}
}
}
}
Question 1
Answer
Question 1
What will be the output of the above program?
a) 32445668
b) 32446687
c) 324456687
d) 32456687
Answer
class A{
public int foo;
}
public class B extends A{
public int bar;
public void setBar(int b){
bar = b;
}
}
Question 2
Answer
Question 2
Which is true about the classes described above?
a) Class A tightly encapsulated
b) Class B is tightly encapsulated
c) Class A and B both are tightly encapsulated
d) Neither Class A nor class B both is tightly encapsulated
Answer
public class TestDivide
{
public static void main(String args[])
{
int value = 0;
try{
int result = 10value;
}finally{
System.out.println("f");
}
}
}
Question 3
Answer
Question 3
What is the result of the above code?
a) Compilation fails since a catch block is not present.
b) Prints only “f” in the output.
c) Only a runtime error is displayed.
d) Prints an “f” in the output and a runtime error is also displayed.
Answer
Question 4
Answer
Question 4
Say that class Rodent has a child class Rat and another child class Mouse.
Class Mouse has a child class PocketMouse. Examine the following:
Rodent rod;
Rat rat = new Rat();
Mouse mos = new Mouse();
PocketMouse pkt = new PocketMouse();
Which of the following will cause a compile error?
a) rod = rat;
b) rod = mos;
c) pkt = null;
d) okt = rat;
Answer
class ParentClass{
public int doStuff(int x){
return x*2;
}
}
public class ChildClass extends ParentClass{
public static void main(String args[]){
ChildClass cc = new ChildClass();
long x = cc.doStuff(7);
System.out.println("x= " + x);
}
public long doStuff(int x){
return x*3;
}
}
Question 5
Answer
Question 5
What is the result of the above code?
a) x = 14
b) x = 21
c) Compilation fails at line 2
d) Compilation fails at line 14
Answer
Also Checkout: TCS NQT Sample Question Paper
public class TestArray
{
public static void main(String args[])
{
int array[][] = new int[2][2];
for(int m=0; m<2; m++)
for(int n=0; n<2; n++)
if(m == n) array[m][n] = 1;
else array[m][n] = 0;
for(int m=0; m<2; m++){
for(int n=0; n<2; n++){
System.out.print(array[m][n]);
}
}
}
}
Question 6
Answer
Question 6
What will be the output on executing the above code?
a) 0
b) 110
c) 1001
d) 1111
Answer
public interface HeavenlyBody { Sting describe(); }
class Star implements HeavenlyBody {
String starName;
public String describe() { return "star" + starName; }
}
class Planet {
String name;
Star orbiting;
public String describe() {
return "planet " + name + "orbiting " +orbiting.describe();
}
}
Question 7
Answer
Question 7
Which statement is true for the above code?
a) The code will fail to compile.
b) The use of aggregation is justified. Since Planet has-a Star.
c) The code will fail to compile if the name starName is replaced with
d) An instance of Planet is a valid instance of a HeavenlyBody.
Answer
abstract class Myclass {
void int() {}
abstract int calculate();
}
class MyImpl extends Myclass {
int calculate() {
System.out.println("Invoking calculate...");
return 1;
}
}
public class TestMyImpl {
public static void main(String[] args) {
MyImpl ml = new MyImpl();
ml.init();
ml.calculate();
}
}
Question 8
Answer
Question 8
What is the expected behavious?
a) Print “Invoking calculate…”
b) Runtime error occurs
c) Compilation error at line 2
d) Compilation error at line 14
Answer
Class Test{
static boolean b1;
public static void main(String[] args){
boolean[] array = new boolean[1];
boolean br = true;
System.out.print(b1+",");
System.out.print(array[0]+",");
System.out.print(b2);
}
}
Question 9
Answer
Question 9
What is the result of attempting to compile and run the program?
a) Prints true,true,true
b) prints false,false,true
c) prints false,null,true
d) compile time error
Answer
C) 0.1
Also Checkout