Java Operators (GATE - CS)
Tests Java operators including bit shift, arithmetic, Math functions, unary operators, and their interaction with switch statements
Questions
What is the output of the given java code?
public class HelloWorld
{
public static void main(String []args)
{
boolean i=1;
switch(Math.round(Math.hypot(114,123))) // Line 1
{
default : System.out.println("Default Case");
case 128<<6<<3 : System.out.println("Case 1");
case 9072/54 : System.out.println("Case 2");
case 128<<4<<2 : System.out.println("Case 3");
}
}
}
- Default Case Case 1 Case 2 Case 3
- Case 1Case 2Case 3
- Case 2Case 3
- Case 3
- Compilation Error
What is the output of the given java program?
public class HelloWorld
{
public static void main(String []args)
{
int i = 1024<<8<<3>>8>>3;
switch(i)
{
default : System.out.println("Default");
case 1048576/(34*32) : System.out.println("Case 1");
case 1073741824/(2*32*4*32*16*8) : System.out.println("Case 2");
case 1048576/(32*32) : System.out.println("Case 3");
}
}
}
- Default Case 1 Case 2 Case 3
- Case 1 Case 2 Case 3
- Case 2 Case 3
- Case 3
- Compiler Error
What is the output of the given java code?
public class HelloWorld
{
public void ret(int a, int b)
{
double res = Math.pow(a,b);
System.out.println(res);
}
public static void main(String []args)
{
HelloWorld h = new HelloWorld();
int i = (1048576>>8>>3>>4>>3);
int y = 1024>>4>>3;
h.ret(i,y);
}
}
- 25686.0
- 25656.0
- 65556.0
- 65536.0
- Compilation error
What is the output of the given java program?
public class HelloWorld
{
public static void main(String []args)
{
int i = 1024<<8<<3>>8>>3; switch(i)
{
default : System.out.
println("Default");
case 1048576/(34*32) : System.out.
println("Case 1");
case 1073741824/(2*32*4*32*16*8*2) : System.out.
println("Case 2");
case 1048576/(32*32) : System.out.
println("Case 3");
}
}
}
- Default Case 1Case 2Case 3
- Case1Case 2Case 3
- Case 2Case 3
- Case 3
- Compiler Error
What is the output of the given java program?
public class HelloWorld
{
public static void main(String []args)
{
int i = 1048576>>8>>3>>8>>3;
switch(i++ - 1) // Line 1
{
case "-1" : System.out.
println("Case -1");
case "1" : System.out.
println("Case 1");
default : break;
case "0" : System.out.
println("Case 0");
}
}
}
- Case -1Case 1Case 0
- Case -1Case 1
- Case 0
- Case 1
- Compiler error.
What is the output of the given java code?
public class HelloWorld
{
public static void main(String []args)
{
System.out.println( Math.round(Math.hypot(4+1,9-1)));
}
}
- 13
- 9
- 12
- 16
- 22
What is the output of the given Java Program?
public class HelloWorld
{
public static void main(String []args)
{
int i = 1048576>>8>>3>>8>>3;
switch(i++ - 1) // Line 1
{
case -1 : System.out.println("Case -1");
case 1 : System.out.println("Case 1");
default : break;
case 0 : System.out.println("Case 0");
}
}
}
- Case -1 Case 1
- Case -1Case 1Case 0
- Case 0
- No output
- Error at Line 1
What is the output of the given java code?
public class HelloWorld
{
public void ret(int a, int b)
{
double res = Math.pow(a,b);
System.out.println(res);
}
public static void main(String []args)
{
HelloWorld h = new HelloWorld();
int i = (1048576>>8>>3>>4>>3);
int y = 1024>>4>>3;
h.ret(i,y);
System.out.println(i^y);
- 65536.0 12
- 65536.0 16
- 25536.012
- 25536.012
- Compiler Error
What is the output of the given java code?
public class HelloWorld
{
public static void main(String []args)
{
int i;
switch(i) // Line 1
{
default : System.out.println("No Value");
case 0 : System.out.println("i is 0");
case 1 : System.out.println("i is 1");
}
}
}
- No Valuei is 0i is 1
- i is 0i is 1
- i is 1
- No output
- Compilation Error
What is the output of the given java program?
public class HelloWorld
{
public static void main(String []args)
{
int i = 1048576>>8>>3>>8>>3;
switch(1 + i++ - 1) // Line 1
{
case 0 : System.out.println(1024<<8<<3>>8>>3);
case -1 : System.out.println(1024<<2>>3<<2);
default : break;
case 1 : System.out.println(1024<<3<<8>>3>>3);
}
- 32768
- 10242048
- 2048
- No Output
- Error at Line 1
What is the output of the given java program?
public class HelloWorld
{
public static void main(String []args)
{
int i = (1048576>>8>>3>>8>>3) + + - + (1024<<8<<3>>8>>3);
switch(i) // Line 1
{
default : System.out.println("Good Bye");
case 1024 : System.out.println(1024<<8<<3>>8>>3);
case 512 : System.out.println(1024<<2>>3<<2);
case 1 : System.out.println(1024<<3<<8>>3>>3);
}
}
}
- Good Bye 1024 2048 32768
- 1024 2048 32768
- 2048 32768
- 32768
- Compilation error
What is the output of the given java program?
public class HelloWorld
{
public static void main(String []args)
{
int i= (int)Math.round(Math.hypot(114,123));
switch(i) // Line 1 { default : System.out.println("Default Case");
case 128<<6<<3 : System.out.println("Case 1");
case 9072/54 : System.out.
println("Case 2"); case 128<<4<<2 :
System.out.println("Case 3");
}
}
}
- Default CaseCase 1Case 2Case 3
- Case 1Case 2Case 3
- Case 2Case 3
- Case 3
- Compilation Error
What is the output of the given java program?
public class HelloWorld
{
public static
void main(String []args)
{
int i = (1048576>>8>>3>>8>>3) + + - + (1024<<8<<3>>8>>3);
switch(i++) // Line 1 { case 1024 : System.out.
println("Hello"); case 1025 : System.out.
println("How r u?"); case 512 : System.out.
println("Bye"); case 513 : System.out.
println("Bye Bye"); default : System.out.
println("Good Bye");
}
}
}
- HelloHow r u?Bye Bye ByeGood Bye
- How r u? Bye Bye Bye Good Bye
- Bye Bye ByeGood Bye
- Bye ByeGood Bye
- Good Bye
What is the output of the given java program?
public class HelloWorld
{
public static void main(String []args)
{
int i = 1024<<8<<3>>8>>3;
switch(i)
{
default : break;
case 1048576/(34*32) : System.out.println("Case 1");
case 1073741824/(2*32*4*32*16*8*2) : System.out.println("Case 2");
case 1048576/(32*30*4) : System.out.println("Case 3");
}
}
}
- Case 1Case 2Case 3
- Case 2 Case 3
- Case 3
- No output
- Compiler Error
What is the output of the given java code?
public class HelloWorld
{
public static
void main(String []args)
{
int i=1;
if(i) switch(i++)
// Line 1 { default : System.out.println("No Value");
case 0 : System.out.
println("i is 0");
case 1 : System.out.
println("i is 1");
case 2 : System.out.
println("i is 2");
}
}
- Valuei is 0i is 1i is 2
- i is 0i is 1i is 2
- i is 1i is 2
- i is 2
- Compilation Error