• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
Switch to Dark Mode

Java Class and Object

Last Updated : Mar 22, 2024
Discuss
Comments

Question 1

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

Question 2

What is the output of the below Java code?

Java
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();

    }
}


  • A

    Compile error

  • B
    values of obj1: 
    a = 11 b = 21
    values of obj2:
    a = 11 b = 21
  • C
    values of obj1: 
    a = 11 b = 21
    values of obj2:
    a = 10 b = 20
  • D
    values of obj1: 
    a = 11 b = 20
    values of obj2:
    a = 10 b = 21
  • E

    Run time error

Question 3

Predict the output of following Java program.


Java
 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();

    }
}
  • A
    a = 1
    b = 2
  • B

    Compilation error

  • C
    b = 2
    a = 1

Question 4

Predict the output of the following program. Java
 
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();
    }
}
  • A
    Compilation error
  • B
    Inside First
    Inside Second
  • C
    Inside First
    Inside First
  • D
    Runtime error

Question 5

Predict the output of the following program. Java
 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);

    }
}
  • A
    obj1.a = 1  obj1.b = 2
    obj2.a = 4  obj2.b = 3
  • B
    obj1.a = 4  obj1.b = 3
    obj2.a = 4  obj2.b = 3
  • C
    Compilation error

Question 6

Predict the output of the following program. Java
 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);

    }
}
  • A
    obj1.a = 1  obj1.b = 2
    obj2.a = 4  obj2.b = 3
  • B
    obj1.a = 4  obj1.b = 3
    obj2.a = 4  obj2.b = 3
  • C
    Compilation error

Question 7

What is the output of the following Java program?

Java
public class Good {
    private int m;

    public Good(int m) {
        this.m = m;
    }

    public boolean equals(Good n) {
        return n.m == m;
    }

    public static void main(String args[]) {
        Good m1 = new Good(22);
        Good m2 = new Good(22);
        Object S1 = new Good(22);
        Object S2 = new Good(22);

        System.out.println(m1.equals(m2));   
        System.out.println(m1.equals(S2));   
        System.out.println(m1.equals(S2));   
        System.out.println(S1.equals(m2));   
    }
}
  • A

    True, True, False, False

  • B

    True, False, True, False

  • C

    True, False, False, True

  • D

    True, False, False, False

Question 8

What is the output of the following Java program?

Java
class Simple {
    public static void main(String[] args) {
        Simple obj = new Simple();
        obj.start();
    }

    void start() {
        long[] P = {3, 4, 5};
        long[] Q = method(P);
        System.out.print(P[0] + P[1] + P[2] + ":");
        System.out.print(Q[0] + Q[1] + Q[2]);
    }

    long[] method(long[] R) {
        R[1] = 7;
        return R;
    }
}
  • A

    12 : 15

  • B

    15 : 12

  • C

    12 : 12

  • D

    15 : 15

Question 9

What is the output of the following Java program?

Java
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";
    }
}
  • A

    dogood : dogoodgood

  • B

    dogood : gooddogood

  • C

    dogood : dodogood

  • D

    dogood : do good

Question 10

Java uses threads to enable the entire environment to be ______.
  • A
    Symmetric
  • B
    Asymmetric
  • C
    Synchronous
  • D
    Asynchronous

Master JAVA and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!

There are 12 questions to complete.

Take a part in the ongoing discussion