• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Java Class and Object

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); 
}  
  • 0
  • garbage value
  • compiler error
  • runtime error

Question 2

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

    }
}
  • Compile error
  • values of obj1: 
    a = 11 b = 21
    values of obj2: 
    a = 11 b = 21
    
  • values of obj1: 
    a = 11 b = 21
    values of obj2: 
    a = 10 b = 20
    
  • values of obj1: 
    a = 11 b = 20
    values of obj2: 
    a = 10 b = 21
    
  • 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 = 1
    b = 2
  • Compilation error
  • 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();
    }
}
  • Compilation error
  • Inside First
    Inside Second
  • Inside First
    Inside First
  • 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);

    }
}
  • obj1.a = 1  obj1.b = 2
    obj2.a = 4  obj2.b = 3
  • obj1.a = 4  obj1.b = 3
    obj2.a = 4  obj2.b = 3
  • 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);

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

Question 7

What is the output of the following JAVA program? 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)); } }
  • True, True, False, False
  • True, False, True, False
  • True, True, False, True
  • True, False, False, False
  • None o the above.

Question 8

What is the output of the following JAVA program ?
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;
}
} //end of class
  • 12 : 15
  • 15 : 12
  • 12 : 12
  • 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;
    }
}
  • dogood : dogoodgood
  • dogood : gooddogood
  • dogood : dodogood
  • dogood : dogood

Question 10

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

There are 12 questions to complete.

Last Updated :
Take a part in the ongoing discussion