1

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);  
    }   
}
A
Compiler Error: Operator >> cannot be applied to negative numbers
B
-2
2
C
2
2
D
0
2
Operators    
Discuss it


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);  
   }   
} 
A
7
3
1
B
15
7
3
C
0
0
0
D
1
1
1
Operators    
Discuss it


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); 
   }  
}
A
30GeeksQuiz
GeeksQuiz30
B
1020GeeksQuiz
GeeksQuiz1020
C
30GeeksQuiz
GeeksQuiz1020
D
1020GeeksQuiz
GeeksQuiz30
Operators    
Discuss it


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);
   } 
}
A
10*20GeeksQuiz
GeeksQuiz10*20
B
200GeeksQuiz
GeeksQuiz200
C
200GeeksQuiz
GeeksQuiz10*20
D
1020GeeksQuiz
GeeksQuiz220
Operators    
Discuss it


Question 4 Explanation: 
Precedence of * is more than +.
Question 5
Which of the following is not an operator in Java?
A
instanceof
B
sizeof
C
new
D
>>>=
Operators    
Discuss it


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);
   }
}
A
true
B
false
Operators    
Discuss it


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);
    }
}
A
true
B
false
C
compiler error
D
throws an exception
Operators    
Discuss it


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

    }
}

A
Compile Error
B
Run Time Error
C
Segmentation Fault
Operators    
Discuss it


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.
 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();
    }
}
 
A
2
B
3
C
4
Operators    
Discuss it


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.
There are 9 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice