Open In App

Output of Java program | Set 26

Last Updated : 11 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Ques1: What is the output of this program?




class A {
    public int i;
    private int j;
}
  
class B extends A {
    void display()
    {
        super.j = super.i + 1;
        System.out.println(super.i + " " + super.j);
    }
}
  
class inheritance {
    public static void main(String args[])
    {
        B obj = new B();
        obj.i = 1;
        obj.j = 2;
        obj.display();
    }
}


a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error

 Answer: d 

Explanation: Class A contains a private member variable j, this cannot be inherited by subclass B. So in class B we can not access j. So it will give a compile time error.

Ques2: What is the output of this program?




class selection_statements {
    public static void main(String args[])
    {
        int var1 = 5;
        int var2 = 6;
        if ((var2 = 1) == var1)
            System.out.print(var2);
        else
            System.out.print(++var2);
    }
}


options:a) 1
b) 2
c) 3
d) 4

Answer: b

Explanation: In “If” statement, first var2 is initialized to 1 and then condition is checked whether var2 is equal to var1. As we know var1 is 5 and var2 is 1, so condition will be false and else part will be executed.

Ques3: What is the output of this program?




class comma_operator {
    public static void main(String args[])
    {
        int sum = 0;
        for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
            sum += i;
        System.out.println(sum);
    }
}


options:
a) 5
b) 6
c) 14
d) compilation error

Answer: b

Explanation: Using comma operator, we can include more than one statement in the initialization and iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value : 0, 1, 2, 3, and j gets the values : 0, 1, 2, 3, 4, 5.

Ques4. What will be the output of the program?




class Geeks {
    public static void main(String[] args)
    {
        Geeks g = new Geeks();
        g.start();
    }
  
    void start()
    {
        long[] a1 = { 3, 4, 5 };
        long[] a2 = fix(a1);
        System.out.print(a1[0] + a1[1] + a1[2] + " ");
        System.out.println(a2[0] + a2[1] + a2[2]);
    }
  
    long[] fix(long[] a3)
    {
        a3[1] = 7;
        return a3;
    }
}


options:
a) 12 15
b) 15 15.
c) 3 4 5 3 7 5
d) 3 7 5 3 7 5

Answer: b

Explanation: The reference variables a1 and a3 refer to the same long array object. When fix() method is called, array a1 is passed as reference. Hence the value of a3[1] becomes 7 which will be reflected in a1[] as well because of call by reference. So the a1[] array become {3, 7, 5}. WHen this is returned to a2[], it becomes {3, 7, 5} as well.

So Output: 3 + 7 + 5 + ” ” + 3 + 7 + 5 = 15 15

Ques5. What will be the output of the program?




class BitShift {
    public static void main(String[] args)
    {
        int x = 0x80000000;
        System.out.print(x + " and  ");
        x = x >>> 31;
        System.out.println(x);
    }
}


options:
a) -2147483648 and 1
b) 0x80000000 and 0x00000001
c) -2147483648 and -1
d) 1 and -2147483648

Answer: a

Explanation: Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this:

Before: 1000 0000 0000 0000 0000 0000 0000 0000
After: 0000 0000 0000 0000 0000 0000 0000 0001

Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads