0% found this document useful (0 votes)
16 views

Java Week 1 questions

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java Week 1 questions

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is the result?

public class Test {


public static void main(String [] args) {
int x = 5;
boolean b1 = true;
boolean b2 = false;

if ((x == 4) && !b2 )


System.out.print("1 ");
System.out.print("2 ");
if ((b2 = true) && b1 )
System.out.print("3 ");
}
}

CHOICE :

A. 2
B. 3
C. 1 2
D. 2 3
E. 1 2 3
F. Compilation fails.
G. An exception is thrown at runtime.

Answer: D

2. What is the result?


class Hexy {
public static void main(String[] args) {
Integer i = 42;
String s = (i<40)?"life":(i>50)?"universe":"everything";
System.out.println(s);
}}

A. null
B. life
C. universe
D. everything
E. Compilation fails.
F. An exception is thrown at runtime.

Answer:
-> D is correct. This is a ternary nested in a ternary with a little unboxing thrown in.
Both of the ternary expressions are false.
-> A, B, C, E, and F are incorrect based on the above.
3. What is the result?
class bitwise_operator {
public static void main(String args[])
{
int var1 = 42;
int var2 = ~var1;
System.out.print(var1 + " " + var2);
}
}
A. 42 43
B. 42 -43
C. 42 42
D. 42 -42

Answer : B

4. What will be the output of the program?

class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}

void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}

void twice(int x)
{
x = x*2;
s = x;
}
}

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

Answer: Option B
5. What will be 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
Compilation fails
.

Answer : B

6. 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. 0
B. 7
C. 8
D. 14

Answer : D
7. What gets displayed on the screen when the following program is compiled and run.
Select the one correct answer.

public class test {


public static void main(String args[]) {
int x,y;
x = 3 & 5;
y = 3 | 5;
System.out.println(x + " " + y);
}
}

A. 71
B. 37
C. 17
D. 31
E. 13
F. 73
G. 75

8. What gets displayed on the screen when the following program is compiled and run. Select
the one correct answer.

public class test {


public static void main(String args[]) {
int x, y;

x = 5 >> 2;
y = x >>> 2;
System.out.println(y);
}
}

A. 5
B. 2
C. 80
D. 0
E. 64

9. Write a program to find the largest of three numbers using ternary operator.( Note :
looping statement should not be used)
10.

You might also like