Programming in Java

Tests intermediate Java programming concepts including constructors, inheritance, object comparison, and inner classes

10 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What is the output of the following program?
import java.io.*;
class sample3
{
int a;
int b;
sample3()
{
a=0;
b=0;
}
sample3(int b1,int c1)
{
a=b1; b=c1;
}
final void print()
{
System.out.print(a);
System.out.print(b);
}
}
class sample31 extends sample3
{
int c;
sample31()
{
c=0;
}
sample31(int c)
{
this.c=c; super(4,5);
}
public static void main(String args[])
{
sample3 ob1=new sample3(4,5);
sample31 ob=new sample31();
System.out.print(ob.a);
System.out.print(ob.b);
System.out.print(ob.c);
sample31 ob2=new sample31(5);
System.out.print(ob2.a);
System.out.print(ob2.b);
System.out.print(ob2.c);
}

  1. Cannot call super class constructor
    Call to the super must be first in the constructor.
  2. 000455
  3. 000005
  4. 000450
  5. 000000
Question 2 Multiple Choice (Single Answer)

What is the output of the following program?
import java.io.*;
class sample2
{
int a;
static int b;
sample2() { a=0; b=10;
}
sample2(int a,int b)
{
this.a=a; this.b=b;
}
public void test()
{
System.out.print(a);
System.out.print(b);
}
public static void main(String args[])
{sample2 ob=new sample2();
System.out.print(ob.a);
System.out.print(ob.b);
sample2 ob1=new sample2(4,5);
System.out.print(ob1.a);
System.out.print(ob1.b);
test();
}
}

  1. 01045010
  2. 0104505
  3. 0104545
  4. error:non static object cannot be referenced from static context
  5. 01045
Question 3 Multiple Choice (Single Answer)

Which of the following options provides the output of this program, when executed?
class Sample5
{
private int x, y;
public sample5(int x, int y)
{
x = x;
}
public String toString()
{ return "[" + x +" ," + y +" ]";
}
public static void main(String []args)
{
sample5 point = new sample5(10, 20);
System.out.println(point);
}
}

  1. point
  2. Point
  3. [0, 0]
  4. [10, 0]
  5. [10, 20]
Question 4 Multiple Choice (Single Answer)

What is the output of the following program?
import static java.lang.System.out;
class sample1
{
int a;
public static void main(String args[])
{
System.out.println(a);
a++;
}
}

  1. 0
  2. Error in the importing package
  3. Compile time error
  4. 1
  5. Both 2 and 3
Question 5 Multiple Choice (Single Answer)

class sample6
{
public static void main(String []args)
{
Double i = 10.0;
Double j = 11.0;
Double k = ++i;
//INCRSystem.out.println("k == j is" + (k == j));
System.out.println("k.equals(j) is" + k.equals(j));
}
}
Which of the following options correctly describes the behavior of this program?

  1. When executed, this program printsk == j is falsek.equals(j) is false
  2. When executed, this program printsk == j is truek.equals(j) is false
  3. When executed, this program printsk == j is falsek.equals(j) is true
  4. When executed, this program printsk == j is truek.equals(j) is true
  5. When compiled, the program will result in a compiler error in the line marked with the comment INCR.
Question 6 Multiple Choice (Single Answer)

Which of the following options provides the output of this program, when executed?
class Sample4
{
public static void main(String []args)
{
String s1 =" Hello Java";
String s2 = new String("Hello Java");
String s3 =" Hello Java";
if(s1 == s2)
{
System.out.println("s1 and s2 equal");
}
else
{
System.out.println("s1 and s2 not equal");
}
if(s1 == s3)
{
System.out.println("s1 and s3 equal");
}
else
{
System.out.println("s1 and s3 not equal");
}
}
}

  1. s1 and s2 equal s1 and s3 equal
  2. s1 and s2 equal s1 and s3 not equal
  3. s1 and s2 not equal s1 and s3 not equal
  4. s1 and s2 not equal s1 and s3 equal
  5. None of the above
Question 7 Multiple Choice (Single Answer)

class sample7
{
public static void main(String []args)
{
int []a1 = {1, 2, 3, 4, 5};
int []a2 = {1, 2, 3, 4, 5};
System.out.println("a1 == a2 is" + (a1 == a2));
System.out.println("a1.equals(a2) is" + a1.equals(a2));
System.out.println("Arrays.equals(a1, a2) is" + java.util.Arrays.equals(a1, a2));
}
}

Which of the following options provides the output of this program when executed?

  1. a1 == a2 is falsearr1.equals(arr2) is falseArrays.equals(a1, a2) is true
  2. a1 == a2 is truea1.equals(a2) is falseArrays.equals(arr1, arr2) is true
  3. a1 == a2 is falsearr1.equals(a2) is trueArrays.equals(a1, a2) is true
  4. a1 == a2 is truea1.equals(a2) is trueArrays.equals(a1, a2) is false
  5. a1 == a2 is truea1.equals(a2) is trueArrays.equals(a1, a2) is true
Question 8 Multiple Choice (Single Answer)

class sample9
{
public static void main(String []args)
{
sample s=new sample(); s.walk(); s.fly();
}
}
class CannotFlyException extends Exception
{
}
interface Bird
{
public abstract void fly() throws CannotFlyException;
}
interface Bipe
{
public void walk();
}
abstract class noFlyer
{
public void fly()
{
System.out.print("cannot fly" );//LINE A
}
}
class sample extends noFlyer implements Bird, Bipe //LINE B
{
public void walk()
{
System.out.print("walk" );
}
}

Which of the following options is the output of this program?

  1. Compiler error in line with comment LINE A because fly() does not declare to throw CannotFlyException.
  2. Compiler error in line with comment LINE B because fly() is not defined and hence need to declare it abstract.
  3. It crashes after throwing the exception CannotFlyException.
  4. Walk cannot fly
  5. None of the above
Question 9 Multiple Choice (Single Answer)

class checkoverride
{
public static void main(String []args)
{
sample8 obj = new childsample8();
obj.sample8a(obj);
}
}
class sample8
{
public static void sample8a(sample8 ob)
{
System.out.println("In sample8.sample8a()");
ob.sample8b();
}
public void sample8b()
{
System.out.println("In Base.bar()");
}
}
class childsample8 extends sample8
{
public static void sample8a(sample8 ob)
{
System.out.println("In childsample8.sample8a()");
ob.sample8b();
}
public void sample8b()
{
System.out.println("In childsample8.sample8b()");
}
}

What is the output of this program when executed?

  1. In sample8.sample8a()In sample8.sample8b()
  2. In sample8.sample8a()In childsample8.sample8b()
  3. In childsample8.sample8a()In sample8.sample8b()
  4. In childsample8.sample8a()In childsample8.sample8b()
  5. None of the above
Question 10 Multiple Choice (Single Answer)

class extractsample10a
{
public static void main(String []args)
{
System.out.println(/HERE/);
}
} class sample10 {
static class sample10a
{
public final String text = "hello";
}
}
What is that expression in /HERE/ so that "hello" can be printed?

  1. sample10.new sample10a().text
  2. new sample10.sample10a().text
  3. sample10.sample10a.text
  4. new sample10().sample10a.text
  5. none of the above