Programming in Java
Tests intermediate Java programming concepts including constructors, inheritance, object comparison, and inner classes
Questions
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);
}
- Cannot call super class constructor
Call to the super must be first in the constructor. - 000455
- 000005
- 000450
- 000000
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();
}
}
- 01045010
- 0104505
- 0104545
- error:non static object cannot be referenced from static context
- 01045
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);
}
}
- point
- Point
- [0, 0]
- [10, 0]
- [10, 20]
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++;
}
}
- 0
- Error in the importing package
- Compile time error
- 1
- Both 2 and 3
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?
- When executed, this program printsk == j is falsek.equals(j) is false
- When executed, this program printsk == j is truek.equals(j) is false
- When executed, this program printsk == j is falsek.equals(j) is true
- When executed, this program printsk == j is truek.equals(j) is true
- When compiled, the program will result in a compiler error in the line marked with the comment INCR.
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");
}
}
}
- s1 and s2 equal s1 and s3 equal
- s1 and s2 equal s1 and s3 not equal
- s1 and s2 not equal s1 and s3 not equal
- s1 and s2 not equal s1 and s3 equal
- None of the above
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?
- a1 == a2 is falsearr1.equals(arr2) is falseArrays.equals(a1, a2) is true
- a1 == a2 is truea1.equals(a2) is falseArrays.equals(arr1, arr2) is true
- a1 == a2 is falsearr1.equals(a2) is trueArrays.equals(a1, a2) is true
- a1 == a2 is truea1.equals(a2) is trueArrays.equals(a1, a2) is false
- a1 == a2 is truea1.equals(a2) is trueArrays.equals(a1, a2) is true
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?
- Compiler error in line with comment LINE A because fly() does not declare to throw CannotFlyException.
- Compiler error in line with comment LINE B because fly() is not defined and hence need to declare it abstract.
- It crashes after throwing the exception CannotFlyException.
- Walk cannot fly
- None of the above
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?
- In sample8.sample8a()In sample8.sample8b()
- In sample8.sample8a()In childsample8.sample8b()
- In childsample8.sample8a()In sample8.sample8b()
- In childsample8.sample8a()In childsample8.sample8b()
- None of the above
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?
- sample10.new sample10a().text
- new sample10.sample10a().text
- sample10.sample10a.text
- new sample10().sample10a.text
- none of the above