Java MCQ Questions - Operators & Assignments

This section focuses on the "operators and assignments" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.

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

A. Characters
B. Boolean
C. Numeric
D. Both Numeric & Characters

View Answer


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

A. Both Integers and floating - point numbers
B. Integers
C. Floating - point numbers
D. None of the mentioned

View Answer


3. Decrement operator, −−, decreases the value of variable by what number?

A. 1
B. 2
C. 3
D. 4

View Answer


4. Which of these statements are incorrect?

A. Assignment operators can be used only with numeric and character data type
B. Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms
C. Assignment operators run faster than their equivalent long forms
D. None of the mentioned

View Answer


5. Can 8 byte long data type be automatically type cast to 4 byte float data type?

A. TRUE
B. FALSE
C. Can be true or false
D. can not say

View Answer


6. Evaluate the value of the expression?

  
		   6 - 2 + 10 % 4 + 7
		   

A. 14
B. 12
C. 13
D. 10

View Answer


7. What is/are highest order precedence operator(s) in Java?

A. ( )
B. { }
C. Both A & B
D. None of these

View Answer


8. The && and || operators

A. Compare two boolean values
B. Compare two numeric values
C. Combine two boolean values
D. Combine two numeric values

View Answer


9. Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?

A. ((x < 100) && (x > 1)) && (x < 0)
B. ((x < 100) && (x > 1)) || (x < 0)
C. (1 > x > 100) || (x < 0)
D. 1 < x < 100 || x < 0

View Answer


10. Select from among the following character escape code which is not available in Java.

A. \\
B. \v
C. \a
D. \t

View Answer


11. What will be the output of the program?

class Main {
    public static void main(String [] args)
   {
     Main p = new Main();
     p.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;
   }
}

A. 12 15
B. 15 15
C. 3 7 5 3 7 5
D. 3 4 5 3 7 5

View Answer


12. What will be the output of the program?

class Main {
    public static void main(String [] args)
	{
	 Main p = new Main();
	 p.start();	
	}
	void start()
	{
	 boolean b1 = false;
	 boolean b2 = fix(b1);
	 System.out.println(b1 + " " + b2);
	}
	boolean fix(boolean b1)	
	{
	 b1 = true;
	 return b1;
	}
}

A. true true
B. true false
C. false true
D. false false

View Answer


13. What will be the output of the program?

class Main {
   public static void main(String [] args)
	{
	 Main p = new Main();
	 p.start();
	}
	void start()
	{
 	 String s1 = "s";
	 String s2 = fix(s1);
	 System.out.println(s1 + " " + s2);
	}
	String fix(String s1)
	{
	 s1 = s1 + "st";
	 System.out.print(s1 + " ");
	 return "st";
	}
}

A. s st
B. sst st
C. st s st
D. sst s st

View Answer


14. Which of the following will produce an answer that is closest in value to a double, d, while not being greater than d?

A. (int)Math.min(d);
B. (int)Math.abs(d);
C. (int)Math.max(d);
D. (int)Math.floor(d);

View Answer


15. 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
D. 2
2

View Answer


16. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;

A. 1, 2 & 3
B. 1 & 4
C. 1, 2, 3 & 4
D. 3 & 2

View Answer


17. What is the output of this program?

class Main {
   public static void main(String args[])
	{
	 double var1 = 2 + 4;
	 double var2 = var1 / 4;
	 int var3 = 2 + 4;
	 int var4 = var3 / 4;
	 System.out.print(var2 + " " + var4);
	}
}

A. 0 1
B. 1 1
C. 1.5 1
D. 1.5 1.0

View Answer


18. What will be the output of the program?

class Main {
   public static void main(String [] args)
	{
	 int x=20;
	 String sup = (x < 15) ? "s" : (x < 22)? "t" : "h";
	 System.out.println(sup);
	}
}

A. s
B. t
C. h
D. Compilation fails

View Answer


19. What will be the output of the program?

  
class Bitwise
{
	public static void main(String [] args)
	{
	 int x = 11 & 9;
	 int y = x ^ 3;
	 System.out.println( y | 12 );
	}
}

A. 7
B. 0
C. 14
D. 8

View Answer


20. What is the output of this program?

  
class increment
{
	public static void main(String args[])
	{
	 int g = 5;
	 System.out.print(++g * 8);
	}
}

A. 44
B. 56
C. 48
D. 40

View Answer


21. What is the output of relational operators?

A. Integer
B. Boolean
C. Characters
D. Double

View Answer


22. Which of these is returned by "greater than", "less than" and "equal to" operators?

A. Integers
B. Floating - point numbers
C. Boolean
D. None of the mentioned

View Answer


23. Which of the following operators can operate on a boolean variable?

A. &&
B. ==
C. ?:
D. +=

View Answer


24. Which of these operators can skip evaluating right hand operand?

A. !
B. |
C. &
D. &&

View Answer


25. Which of these statements 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

View Answer


26. 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

View Answer


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

View Answer


28. 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

View Answer


29. 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

View Answer


30. Which of these have highest precedence?

A. ()
B. ++
C. *
D. >>

View Answer


31. 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

View Answer


32. 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

View Answer


33. 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

View Answer


34. 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

View Answer


35. What is the output of this program?

class Main
{
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

View Answer





Also check :

Discussion



* You must be logged in to add comment.