Java Operators

Question 1
Predict the output of following Java Program
class Test {
    public static void main(String args[])  {
       int x = -4;
       System.out.println(x>>1);  
       int y = 4;
       System.out.println(y>>1);  
    }   
}
Cross
Compiler Error: Operator >> cannot be applied to negative numbers
Tick
-2
2
Cross
2
2
Cross
0
2


Question 1-Explanation: 
See http://www.geeksforgeeks.org/bitwise-shift-operators-in-java/
Question 2
Predict the output of following Java program. Assume that int is stored using 32 bits.
class Test {
    public static void main(String args[])  {
       int x = -1;  
       System.out.println(x>>>29);  
       System.out.println(x>>>30);  
       System.out.println(x>>>31);  
   }   
} 
Tick
7
3
1
Cross
15
7
3
Cross
0
0
0
Cross
1
1
1


Question 2-Explanation: 
Please see http://www.geeksforgeeks.org/bitwise-shift-operators-in-java/
Question 3
class Test {
    public static void main(String args[])  {
       System.out.println(10  +  20 + "GeeksQuiz"); 
       System.out.println("GeeksQuiz" + 10 + 20); 
   }  
}
Cross
30GeeksQuiz
GeeksQuiz30
Cross
1020GeeksQuiz
GeeksQuiz1020
Tick
30GeeksQuiz
GeeksQuiz1020
Cross
1020GeeksQuiz
GeeksQuiz30


Question 3-Explanation: 
In the given expressions 10 + 20 + \"GeeksQuiz\" and \"GeeksQuiz\" + 10 + 20 , there are two + operators, so associativity comes to the picture. The + operator is left to right. So the first expression is evaluated as (10 + 20) + \"GeeksQuiz\" and second expression is evaluated as (\"GeeksQuiz\" + 10) + 20 .
Question 4
class Test {
    public static void main(String args[])  {
       System.out.println(10*20 + "GeeksQuiz");
       System.out.println("GeeksQuiz" + 10*20);
   } 
}
Cross
10*20GeeksQuiz
GeeksQuiz10*20
Tick
200GeeksQuiz
GeeksQuiz200
Cross
200GeeksQuiz
GeeksQuiz10*20
Cross
1020GeeksQuiz
GeeksQuiz220


Question 4-Explanation: 
Precedence of * is more than +.
Question 5
Which of the following is not an operator in Java?
Cross
instanceof
Tick
sizeof
Cross
new
Cross
>>>=


Question 5-Explanation: 
There is no sizeof operator in Java. We generally don\'t need size of objects.
Question 6
class Base {}

class Derived extends Base {
   public static void main(String args[]){
      Base a = new Derived();
      System.out.println(a instanceof Derived);
   }
}
Tick
true
Cross
false


Question 6-Explanation: 
The instanceof operator works even when the reference is of base class type.
Question 7
class Test
{
    public static void main(String args[])
    {
        String s1 = "geeksquiz";
        String s2 = "geeksquiz";
        System.out.println("s1 == s2 is:" + s1 == s2);
    }
}
Cross
true
Tick
false
Cross
compiler error
Cross
throws an exception


Question 7-Explanation: 
The output is “false” because in java + operator precedence is more than == operator. So the given expression will be evaluated to “s1 == s2 is:geeksquiz” == “geeksquiz” i.e false.
Question 8
class demo
{
    int a, b, c;
    demo(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
    }
    
    demo()
    {
        a = b = c = 0;
    }
    
    demo operator+(const demo &obj)
    {
        demo object;
        object.a = this.a + obj.a;
        object.b = this.b + obj.b;
        object.c = this.c + obj.c;
        return object;
    }
}

class Test
{
    public static void main(String[] args)
    {

        demo obj1 = new demo(1, 2, 3);
        demo obj2 = new demo(1, 2, 3);
        demo obj3 = new demo();

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

    }
}

Tick
Compile Error
Cross
Run Time Error
Cross
Segmentation Fault


Question 8-Explanation: 
Operator overloading is not support by JAVA. It only supports method overloading, whereas C++ supports both method and operator overloading.
Question 9

Predict the output of the following program. 

Java

class Test
{
    boolean[] array = new boolean[3];
    int count = 0;
  
    void set(boolean[] arr, int x)
    {
        arr[x] = true;
        count++;
    }
  
    void func()
    {
        if(array[0] && array[++count - 2] | array [count - 1])
            count++;
  
        System.out.println("count = " + count);
    }
  
  
    public static void main(String[] args)
    {
        Test object = new Test();
        object.set(object.array, 0);
        object.set(object.array, 1);
        object.func();
    }
}
Cross

2

Cross

3

Tick

4



Question 9-Explanation: 


First call to function set(), sets array[0] = true, array[1] = false and array[2] = false. Second call to function set(), sets array[0] = true, array[1] = true and array[2] = false. In function func(),if statement evaluates to be true. So, count = 4.

Question 10
What is the output of the following Java program?
Class Test
{
 public static void main (String [] args)
 {
 int x = 0;
 int y = 0;
 for (int z = 0; z < 5; z++)
 {
 if((++x > 2) || (++y > 2))
 {
 x++;
 }
 }
 System.out.println( x + " " + y);
 }
}
Tick
8 2
Cross
8 5
Cross
8 3
Cross
5 3


Question 10-Explanation: 
By applying short circuit technique,
z = 0: x = 1, y = 1, if condition false
z = 1: x = 2, y = 2, if condition false
z = 2: x = 3, if condition true and due
to short circuiting ++y is not evaluated,
x++ // x = 4
z = 3: x = 5, again if condition is true,
x++ // x = 6
z = 4: x = 7, condition true, x++ // x = 8
So, option (A) is correct.
There are 13 questions to complete.

  • Last Updated : 10 Mar, 2018

Share your thoughts in the comments
Similar Reads