Java Programs with Output

1) Predict the output.

class T {
int t = 20;
}
class Main {
public static void main(String [ ] args) {
T t1 = new T() ;

System.out.println(t1.t);
}
}

Options:-

1) 20
2) 0
3) Compiler error

Answer

1) 20

2) What will be the output of the following Java code?

class Output {
public static void main(String [ ] args) {
Object obj = new Object();
System
.out.print(obj.getclass( ));
}
}

Options:-

1) Object
2) class Object
3) class java.lang.Object
4) Compilation Error

Answer

3) class java.lang.Object

3) Predict the output of the following Java Program?

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

System.out.println("Derived::show() called");
}
} public class Main {
public static void main(String[ ] args) {
Base b = new Derived();
b.show();
}
}

Options:-

1) Derived::show() called
2) Base::show() called
3) none

Answer

1) Derived::show() called

4) Predict the output of following Java Program

class Grandparent {
public void Print() {
System.out.println( "Parent's Print ( ) ");
}
}
class Parent extends Grandparent {
public void Print() {
System.out.println( "Grandparent's Print ( ) ");
}
}
class Child extends Parent {
public void Print() {
super.super.Print();
System.out.println( "Child's Print ( ) ");
}
}
public class Main {
public static voidmain( String [ ] args) {
Child c = new Child( );
c.Print();
}
}

Options:-

1) Runtime Error
2) Compiler Error in super.super.Print()
3) Grandparent's Print()
4) Parent's Print()

Answer

2) Compiler Error in super.super.Print()

5) What will be the output of the following Java code?

class A {
int i ;
int j ;
A() {
i = 1;
j = 2;
}
}
class Output { public static void main( String args[]) {
A obj1 = new A();
System.out.print(obj1.toString());
}
}

Options:-

1) true
2) false
3) String associated with obj1
4) Compilation Error

Answer

3) String associated with obj1