Java Operators Questions and Answers

Related Topics:

Java Programming : Java Operators

This page consist of Java Operators quiz questions and answers., Java Interview questions. All questions are in the form of MCQs. You can also download the Java Operators Pdf from here.
1. Which of the following can be operands of arithmetic operators?
[A] Numeric
[B] Boolean
[C] Characters
[D] Both Boolean & Characters

Answer: D. Both Boolean & Characters

Explanation:

The operand of arithmetic operators can be any of numeric or character type, But not Boolean.
2. What is the output of the 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

Answer: D. 27,9

Explanation:

Operator ++ has higher precedence than multiplication operator(*). Hence x is incremented to 9 than multiplied with 3 giving 27.
3. What is the output of the program.
public class Test 

{ 

    public static void leftshift(int i, int j) 

    {

  i <<= j; 

    } 

    public static void main(String args[]) 

    {

        int i = 4, j = 2; 

        leftshift(i, j); 

        System.out.printIn(i); 

    } 

}
[A] 2
[B] 4
[C] 8
[D] 16

Answer: B. 4

Explanation:

Java only ever passes arguments to a method by value (i.e. a copy of the variable) and never by reference. Therefore the value of the variable i remains unchanged in the main method.
4. what is the output of the program ?
class Equals 

{

public static void main(String [] args) 

    {

        int x = 100;

        double y = 100.1;

        boolean b = (x = y);

        System.out.println(b);

    }

}
[A] true
[B] false
[C] Compilation fails
[D] An exception is thrown at run time

Answer: C. Compilation fails

Explanation:

The code will not compile because in line
boolean b = (x = y);
The line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.
5. What is the output of the program ?
class Test 

{

public static void main(String [] args) 

{

        int x=20;

       String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";

        System.out.println(sup);

    }

}
[A] small
[B] tiny
[C] huge
[D] compilaton fails

Answer: B. tiny

Explanation:

This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup.
6. 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

Answer: C. Both Integers and floating – point numbers

Explanation:

Modulus operator can be applied to both integers and floating point numbers.
7. Decrement operator, –, decreases value of variable by what number?
[A] 1
[B] 2
[C] 3
[D] 4

Answer: A. 1

8. Which of these statements are incorrect?
[A] Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms.
[B] Assignment operators run faster than their equivalent long forms
[C] Assignment operators can be used only with numeric and character data type
[D] None

Answer: D. None

9. What is the output of this program?
class increment {

        public static void main(String args[]) 

        {        

             int g = 3;

             System.out.print(++g * 8);

        } 

    }
[A] 25
[B] 24
[C] 32
[D] 33

Answer: C. 32

Explanation:

Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
10. Which of these is not a bit-wise operator?
[A] &
[B] &=
[C] |=
[D] <=

Answer: D. <=

Explanation:

<= is a relational operator.

Comments: (Your feedback is valuable to us)

yogesh rawat 1 year ago Reply

Great


Debjit Rowdian Banerjee 1 year ago Reply

dfgdfgvdfg


alpesh sakhanpara 1 year ago Reply

Like mcq


Jignesh Sangale 1 year ago Reply

good


rahul mudgal 1 year ago Reply

good


;