Operators and Control Statements

1- Which of the following can be operands of arithmetic operators?


a) Numeric
b) Boolean
c) Characters
d) Both Numeric & Characters

 


2- Modulus operator, %, can be applied to which of these?


a) Integers
b) Floating – point numbers
c) Both Integers and floating – point numbers.
d) None of the mentioned

 


3- What is the output of this program?

 

    class increment {

        public static void main(String args[])

        {

            double var1 = 1 + 5; 

            double var2 = var1 / 4;

            int var3 = 1 + 5;

            int var4 = var3 / 4;

            System.out.print(var2 + " " + var4); 

        } 

    }

a) 1 1
b) 0 1
c) 1.5 1
d) 1.5 1.0

 


4- What is the output of this program?

 

    class Output {

        public static void main(String args[]) 

        {    

             int x , y;

             x = 10;

             x++;

             --x;

             y = x++;

             System.out.println(x + " " + y);

        } 

    }

a) 11 11
b) 10 10
c) 11 10
d) 10 11

 


5- Which of these is not a bitwise operator?


a) &
b) &=
c) |=
d) <=

 


6- Which of these statements are incorrect?


a) The left shift operator, <<, shifts all of the bite in a value to the left specified number of times. b) The right shift operator, >>, shifts all of the bite in a value to the right specified number of times.
c) The left shift operator can be used as an alternative to multiplying by 2.
d) The right shift operator automatically fills the higher order bits with 0.

 


7- What is the output of this program?

 

    class bitwise_operator {

        public static void main(String args[])

        {

            int var1 = 42;

            int var2 = ~var1;

            System.out.print(var1 + " " + var2);      

        } 

    }

a) 42 42
b) 43 43
c) 42 -43
d) 42 43

 


8- What is the output of this program?

 

    class bitwise_operator {

        public static void main(String args[]) 

        {    

             int a = 3;

             int b = 6;

       int c = a | b;

             int d = a & b;             

             System.out.println(c + " "  + d);

        } 

    }

a) 7 2
b) 7 7
c) 7 5
d) 5 2

 


9- What is the output of this program?

 

    class leftshift_operator {

        public static void main(String args[]) 

        {        

             byte x = 64;

             int i;

             byte y; 

             i = x << 2;

             y = (byte) (x << 2)

             System.out.print(i + " " + y);

        } 

    }

a) 0 64
b) 64 0
c) 0 256
d) 256 0

 


10- What is the output of this program?

 

   class rightshift_operator {

        public static void main(String args[]) 

        {    

             int x; 

             x = 10;

             x = x >> 1;

             System.out.println(x);

        } 

    }

a) 10
b) 5
c) 2
d) 20

 


11- What is the output of this program?

 

    class Output {

        public static void main(String args[]) 

        {    

             int a = 1;

             int b = 2;

             int c = 3;

             a |= 4;

             b >>= 1;

             c <<= 1;

             a ^= c;

             System.out.println(a + " " + b + " " + c);

        } 

    }

a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6

 


12- What is the output of relational operators?


a) Integer
b) Boolean
c) Characters
d) Double

 


13- Which of these operators can skip evaluating right hand operand?


a) !
b) |
c) &
d) &&


14- Which of these statement is correct?


a) true and false are numeric values 1 and 0.
b) true and false are numeric values 0 and 1.
c) true is any non zero value and false is 0.
d) true and false are non numeric values. 

 


15- What is the output of this program?

 

    class Relational_operator {

        public static void main(String args[])

        {

            int var1 = 5; 

            int var2 = 6;

            System.out.print(var1 > var2);

        } 

    }

a) 1
b) 0
c) true
d) false

 


16- What is the output of this program?

 

   class bool_operator {

        public static void main(String args[]) 

        {    

             boolean a = true;

             boolean b = !true;

             boolean c = a | b;

       boolean d = a & b;

             boolean e = d ? b : c;

             System.out.println(d + " " + e);

        } 

    }

a) false false
b) true ture
c) true false
d) false true

 


17- What is the output of this program?

 

    class ternary_operator {

        public static void main(String args[]) 

        {        

             int x = 3;

             int y = ~ x;

             int z;

             z = x > y ? x : y;

             System.out.print(z);

        } 

    }

a) 0
b) 1
c) 3
d) -4

 


18- What is the output of this program?

 

    class Output {

        public static void main(String args[]) 

        {    

             int x , y = 1;

             x = 10;

             if (x != 10 && x / 0 == 0)

                 System.out.println(y);

             else

                 System.out.println(++y);

        } 

    }

a) 1
b) 2
c) Runtime error owing to division by zero in if condition.
d) Unpredictable behavior of program.

 


19- What is the output of this program?

 

    class Output {

        public static void main(String args[]) 

        {    

             boolean a = true;

             boolean b = false;

             boolean c = a ^ b;

             System.out.println(!c);

        } 

    }

a) 0
b) 1
c) false
d) true

 


20- Which of these have highest precedence?


a) ()
b) ++
c) *
d) >>

 


21- What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3


a) Integer
b) Floating – point numbers
c) Boolean
d) None of the mentioned

 


22- What is the value stored in x in following lines of code?


int x, y, z;
x = 0;
y = 1;
x = y = z = 8;


a) 0
b) 1
c) 9
d) 8

 


23-  What is the order of precedence (highest to lowest) of following operators?


1. &
2. ^
3. ?:
a) 1 -> 2 -> 3
b) 2 -> 1 -> 3
c) 3 -> 2 -> 1
d) 2 -> 3 -> 1

 


24- Which of these statements are incorrect?


a) Equal to operator has least precedence.
b) Brackets () have highest precedence.
c) Division operator, /, has higher precedence than multiplication operator.
d) Addition operator, +, and subtraction operator have equal precedence.

 


25- What is the output of this program?

 

    class operators {

        public static void main(String args[])

        {

            int var1 = 5; 

            int var2 = 6;

            int var3;

            var3 = ++ var2 * var1 / var2 + var2;

            System.out.print(var3);

        } 

    }

a) 10
b) 11
c) 12
d) 56

 


26- What is the output of this program?

 

    class operators {

        public static void main(String args[]) 

        {    

             int x = 8;

             System.out.println(++x * 3 + " " + x);

        } 

    }

a) 24 8
b) 24 9
c) 27 8
d) 27 9

 


27- What is the output of this program?

 

    class ternary_operator {

        public static void main(String args[]) 

        {        

             int x = 3;

             int y = ~ x;

             int z;

             z = x > y ? x : y;

             System.out.print(z);

        } 

    }

a) 0
b) 1
c) 3
d) -4

 


28- Which of these lines of code will give better performance?


1. a | 4 + c >> b & 7;
2. (a | ((( 4 * c ) >> b ) & 7 ))
a) 1 will give better performance as it has no parentheses.
b) 2 will give better performance as it has parentheses.
c) Both 1 & 2 will give equal performance.
d) Dependent on the computer system.

 


29- What is the output of this program?

 

   class Output {

        public static void main(String args[]) 

        {    

             int x , y = 1;

             x = 10;

             if (x != 10 && x / 0 == 0)

                 System.out.println(y);

             else

                 System.out.println(++y);

        } 

    }

a) 1
b) 2
c) Runtime error owing to division by zero in if condition.
d) Unpredictable behavior of program.

 


30- Which of these are selection statements in Java?


a) if()
b) for()
c) continue
d) break

 


31- Which of these jump statements can skip processing remainder of code in its body for a particular iteration?


a) break
b) return
c) exit
d) continue

 


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

        } 

    }

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

 


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

        } 

    }

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

 


34- What is the output of this program?

 

    class jump_statments {

        public static void main(String args[]) 

        {        

             int x = 2;

             int y = 0;

             for ( ; y < 10; ++y) {

                 if (y % x == 0) 

                     continue;  

                 else if (y == 8)

                      break;

                 else

                    System.out.print(y + " ");

             }

        } 

    }

a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9

 


35- What is the output of this program?

 

    class Output {

        public static void main(String args[]) 

        {    

             int a = 5;

             int b = 10;

             first: {

                second: {

                   third: { 

                       if (a ==  b >> 1)

                           break second;

                          }

                   System.out.println(a);

                }

                System.out.println(b);

             }

        } 

    }

a) 5 10
b) 10 5
c) 5
d) 10


 

Related Articles

post a comment