Java Class and Object

Question 1
Predict the output of following Java program?
class Test {
  int i;
} 
class Main {
   public static void main(String args[]) { 
     Test t; 
     System.out.println(t.i); 
}  
Cross
0
Cross
garbage value
Tick
compiler error
Cross
runtime error


Question 1-Explanation: 
t is just a reference, the object referred by t is not allocated any memory. Unlike C++, in Java all non-primitive objects must be explicitly allocated and these objects are allocated on heap. The following is corrected program.
Question 2
Predict the output of following Java program
class Test {
  int i;
} 
class Main {
  public static void main(String args[]) { 
      Test t = new Test(); 
      System.out.println(t.i);
   } 
}
Cross
garbage value
Tick
0
Cross
compiler error
Cross
runtime error


Question 2-Explanation: 
In Java, fields of classes and objects that do not have an explicit initializer and elements of arrays are automatically initialized with the default value for their type (false for boolean, 0 for all numerical types, null for all reference types). Local variables in Java must be definitely assigned to before they are accessed, or it is a compile error.
Question 3
class demo
{
    int a, b;
    
    demo()
    {
        a = 10;
        b = 20;
    }
    
    public void print()
    {
        System.out.println ("a = " + a + " b = " + b + "\n");
    }
}

class Test
{

    public static void main(String[] args)
    {
        demo obj1 = new demo();
        demo obj2 = obj1;

        obj1.a += 1;
        obj1.b += 1;

        System.out.println ("values of obj1 : ");
        obj1.print();
        System.out.println ("values of obj2 : ");
        obj2.print();

    }
}

Cross
Compile error
Tick
values of obj1: 
a = 11 b = 21
values of obj2: 
a = 11 b = 21
Cross
values of obj1: 
a = 11 b = 21
values of obj2: 
a = 10 b = 20
Cross
values of obj1: 
a = 11 b = 20
values of obj2: 
a = 10 b = 21
Cross
Run time error


Question 3-Explanation: 
Assignment of obj2 to obj1 makes obj2 a reference to obj1. Therefore, any change in obj1 will be reflected in obj2 also.
Question 4
Predict the output of following Java program.
 class demoClass
{
    int a = 1;

    void func()
    {
        demo obj = new demo();
        obj.display();
    }


    class demo
    {
        int b = 2;

        void display()
        {
            System.out.println("\na = " + a);
        }
    }

    void get()
    {
        System.out.println("\nb = " + b);
    }
}


class Test
{
    public static void main(String[] args)
    {
        demoClass obj = new demoClass();
        obj.func();
        obj.get();

    }
}


Cross
a = 1
b = 2
Tick
Compilation error
Cross
b = 2
a = 1


Question 4-Explanation: 

Members of inner class ‘demo’ can not be used in the outer class ‘Test’. Thus, get() of outer class can not access variable ‘b’ of inner class.
Question 5
Predict the output of the following program.
 
class First
{

    void display()
    {
        System.out.println("Inside First");
    }
}

class Second extends First
{

    void display()
    {
        System.out.println("Inside Second");
    }
}


class Test
{

    public static void main(String[] args)
    {
        First obj1 =  new First();
        Second obj2 =  new Second();

        First ref;
        ref = obj1;
        ref.display();

        ref = obj2;
        ref.display();
    }
}

Cross
Compilation error
Tick
Inside First
Inside Second
Cross
Inside First
Inside First
Cross
Runtime error


Question 5-Explanation: 
‘ref’ is a reference variable which obtains the reference of object of class First and calls its function display(). Then ‘ref’ refers to object of class Second and calls its function display().
Question 6
Predict the output of the following program.
 class Test
{
    int a = 1;
    int b = 2;

    Test func(Test obj)
    {
        Test obj3 = new Test();
        obj3 = obj;
        obj3.a = obj.a++ + ++obj.b;
        obj.b = obj.b;
        return obj3;
    }

    public static void main(String[] args)
    {
        Test obj1 = new Test();
        Test obj2 = obj1.func(obj1);

        System.out.println("obj1.a = " + obj1.a + "  obj1.b = " + obj1.b);
        System.out.println("obj2.a = " + obj2.a + "  obj1.b = " + obj2.b);

    }
}

Cross
obj1.a = 1  obj1.b = 2
obj2.a = 4  obj2.b = 3
Tick
obj1.a = 4  obj1.b = 3
obj2.a = 4  obj2.b = 3
Cross
Compilation error


Question 6-Explanation: 

obj1 and obj2 refer to same memory address.
Question 7
Predict the output of the following program.
 class Test
{
    int a = 1;
    int b = 2;

    Test func(Test obj)
    {
        Test obj3 = new Test();
        obj3 = obj;
        obj3.a = obj.a++ + ++obj.b;
        obj.b = obj.b;
        return obj3;
    }

    public static void main(String[] args)
    {
        Test obj1 = new Test();
        Test obj2 = obj1.func(obj1);

        System.out.println("obj1.a = " + obj1.a + "  obj1.b = " + obj1.b);
        System.out.println("obj2.a = " + obj2.a + "  obj1.b = " + obj2.b);

    }
}

Cross
obj1.a = 1  obj1.b = 2
obj2.a = 4  obj2.b = 3
Tick
obj1.a = 4  obj1.b = 3
obj2.a = 4  obj2.b = 3
Cross
Compilation error


Question 7-Explanation: 

obj1 and obj2 refer to same memory address.
Question 8
What is the output of the following JAVA program ?
Class Test {
    public static void main(String[] args) {
        Test obj = new Test();
        obj.start();
    }
    void start() {
        String stra = ”do”;
            String strb = method(stra);
        System.out.print(“: ”+stra + strb);
    }
    String method(String stra) {
        stra = stra + ”good”;
        System.out.print(stra);
        return“ good”;
    }
}
Cross
dogood : dogoodgood
Cross
dogood : gooddogood
Cross
dogood : dodogood
Tick
dogood : dogood


Question 9
Java uses threads to enable the entire environment to be ______.
Cross
Symmetric
Cross
Asymmetric
Cross
Synchronous
Tick
Asynchronous


Question 9-Explanation: 
Java uses threads to enable the entire environment to be asynchronous. Asynchronous threading is pre-emptive i.e. a thread once start executing a task it can hold it in mid, save the current state and start executing another task (context switching) according to priority and other specified criteria and threading. So, option (D) is correct.
Question 10
In Java, when we implement an interface method, it must be declared as:
Cross
Private
Cross
Protected
Tick
Public
Cross
Friend


Question 10-Explanation: 
In Java, when we implement an interface method, it must be declared as Public. For more information on Java interface Refer:Interfaces in Java option (C) is correct.
There are 12 questions to complete.


  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads