0% found this document useful (0 votes)
16 views16 pages

Test 1

The document contains a series of Java programming questions and multiple-choice answers related to concepts such as boolean variables, switch statements, and data types. Each question assesses the reader's understanding of Java syntax and behavior, with options ranging from expected outputs to error types. It serves as a quiz or practice test for individuals learning Java basics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

Test 1

The document contains a series of Java programming questions and multiple-choice answers related to concepts such as boolean variables, switch statements, and data types. Each question assesses the reader's understanding of Java syntax and behavior, with options ranging from expected outputs to error types. It serves as a quiz or practice test for individuals learning Java basics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

JavaBasics

All Questions Must be Answered

1. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
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 ");
}
}

2

3

12

123

23

Compilation Fails

An exception is thrown at
⚪ runtime

2. Question (1
Which of these values can a boolean variable contain? poi
1. true nt)
2. false
3. 0
4. 1
Only 2

Only 1

1 and 4

1 and 2

3. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
float f = 12;
switch (f) { // Line 1
case 10 + 1: // Line 2
System.out.println("Twelve");
case 0: // Line 3
System.out.println("Zero");
case (int) 12.0:
System.out.println("Decimal");
default:
System.out.println("Default");
}
}
}

Compiles successfully without


⚪ any error
Compilation error in Line 3
⚪ "Cannot use 0 case"
Compilation error in Line 2
⚪ "Cannot use operator in case"
Compilation error in Line 1 "
⚪ Cannot use float type in switch"

4. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int i = 1;
int j = 0;
switch (i) {
case 1:
j = j + 2;
case 2:
++j;
break;
case 3:
j++;
default:
j = 5;
break;
}
System.out.println(j);
}
}

3

5

4

2

5. If switch feature is used, then (1


poi
nt)

default case must be present



default case, if used , should be
⚪ the last case
default case, if used, can be
⚪ placed anywhere
none of the above

6. Question (1
What will be the output of the below code? poi
public static void main(String args[]) { nt)
int age;
System.out.println(age);
}

0

Garbage Value

Compilation Error

2147483647

7. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int i = -1, j = -1;
switch (i) {
case -1:
j = 1;
case 2:
j = 2;
break;
default:
j = 0;
}
System.out.println("j = " + j);
}
}

j=2

j=1

j=0

Compilation Error

8. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
double i;
char j = 'b';
switch (j) {
case 'a':
case 'A':
i = 7.5;
break;
case 'b':
case 'B':
i = 5.5;
break;
case 'c':
case 'C':
i = 2.5;
break;
default:
i = 0.5;
}
System.out.println(i);
}
}

Compilation error as each block


⚪ must have at least one
statement
Compilation error as variable i
⚪ is not initialized while
declaration
0.5

5.5

9. Question (1
What is the result of compiling and running the following code? poi
public static void main(String[] args) { nt)
int x = 5, y;
while (++x < 7) {
y = 2;
}
System.out.println(x + y);
}

7

8

9

Compilation Error

10. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int k = 2;
switch (k) {
case 'a':
System.out.println("Welcome");
case 2:
System.out.println("To");
case 'b':
System.out.println("Infosys");
break;
default:
System.out.println("Hello");
}
}
}

Welcome To Infosys Hello



To Infosys

To

Hello

11. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
boolean x = true;
boolean y = false;
if (x && y) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
true

false

true false

Compilation Error

12. Which of the following options correctly relates / and % (1


poi
nt)

b = (a/b) * b + a%b

a = (a/b) * b + a%b

b = (a%b) * b + a/b

a = (a%b) * b + a/b

13. Question (1
What should be the value of num1 and num2 to get the output as 2? poi
public class Demo { nt)
public static void main(String[] args) {
int num1;
int num2;
if ((num1 / num2 == 5) && (num1 + num2) > 5) {
System.out.println("1");
} else if ((num1 - num2) >= 1 || (num1 % num2) == 0) {
System.out.println("2");
} else {
System.out.println("3");
}
}
}

num1 = -10, num2 = 3



num1 = 5, num2 = 1

num1 = 0, num2 = 5

num1 = 11, num2 = 2

14. Question (1
Find the output of the following program. poi
public class Solution{ nt)
public static void main(String[] args){
byte x = 127;
x++;
x++;
System.out.print(x);
}
}

-127

127

129

2

15. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int num1 = 25;
int num2 = 34;
if (num1 / 3 >= num2 / 4) {
num1 = num1 + 1;
} else {
num2 = num2 + 1;
}
System.out.println(num1 + "," + num2);
}
}

25,35

25,34

26,34

26,36

16. Question (1
public class Tester { poi
static { nt)
int x = 3;
}
static int x;
public static void main(String[] args) {
x--; // line 7
System.out.println(x);
}
}

3

2

-1

Compilation error at line 7, x is
⚪ not initialized

17. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int i = 10;
if(i) {
System.out.println("Hello");
}
else {
System.out.println("Bye");
}
}
}

Hello

Bye

Compilation Error

Runtime Error

18. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int a = 12 * 3 - 9 / 2;
int b = 14 * 4 + 175 / 8;
if (a++ % 2 == 0) {
if (b-- % 4 == 0) {
System.out.println("a = " + a + " b = " + b);
} else {
System.out.println("a = " + a + " b = " + b);
}
} else {
System.out.println("a = " + a + " b = " + --b);
}
}
}

a=32 b=76

a=33 b=76

a=33 b=77

a=32 b=77

19. Question (1
Find the output of the following code. poi
int Integer = 24; nt)
char String = ‘I’;
System.out.print(Integer);
System.out.print(String);

Compilation Error

Throws Exception

I

24I

20. Question (1
Which of the following codes will compile and run properly? poi
a. public class Test1 { nt)
public static void main() {
System.out.println("Test1");
}
}
b. public class Test2 {
static public void main(String[] in) {
System.out.println("Test2");
}
}
c. public class Test3 {
public static void main(String args) {
System.out.println("Test3");
}
}
d. public class Test4 {
staticint main(String args[]) {
System.out.println("Test4");
}
}
e. public class Test5 {
static void main(String[] data) {
System.out.println("Test5");
}
}

a only

b only

a and b only

All are

21. Question (1
class ExampleFive { poi
public static void main(String[] args) { nt)
final int i = 22;
byte b = i;
System.out.println(i + ", " + b);
}
}

Prints: 22, 20

Runtime Error: Cannot type
⚪ cast int to byte
Prints: 22, 22

Compile Time Error: Loss of
⚪ precision

22. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int a = 10;
int b = 2;
System.out.println((a < b) ? a++ : --b);
}
}

2

1

10

11

23. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int num1 = 100;
int num2 = 200;
int num3 = 6;
if (5 >= num3) {
if (num1 > 100 || num2 > 150) {
System.out.println("1");
}
} else if (num1 >= 100 && num2 > 150) {
System.out.println("2");
} else {
System.out.println("3");
}
}
}

1

2

3

13

24. The purpose of the following program (1


b = s + b; poi
s = b - s; nt)
b = b - s;
where s, b are two integers is to

exchange (swap) the contents


⚪ of s and b
transfer contents of s to b

transfer contents of b to s

negate the contents of s and b

25. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int k = 1;
switch (k) {
default:
System.out.println("Have");
case 'a':
System.out.println("A");
case 'b':
System.out.println("Good Day");
}
}
}

Hava A

Compilation Error

Have

Have A Good Day

26. Question (1
Find the output of the following program. poi
public class Solution{ nt)
public static void main(String[] args){
short x = 10;
x = x * 5;
System.out.print(x);
}
}

50

10

Compilation Error

Runtime Error

27. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int num1 = 20;
double num2 = 0;
if (num1 >= 20)
num2 = 1.5;
if (num1 < 30)
num2 = 2;
System.out.println("Value of num2 is " + num2);
}
}

Value of num2 is 2.0



Value of num2 is 2

Value of num2 is 1.5

Runtime Error

28. Question (1
. Number of primitive data types in Java are? poi
nt)

6

7

9

8

29. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int a = -10;
int b = -200;
int c = 2000;
int d = 4000;
if (a * b >= d) {
if (d > c) {
if (d % c != 0) {
System.out.println(11);
} else {
System.out.println(22);
}
}
} else {
if (b / a > 0) {
if (a < b || d % c != 0) {
System.out.println(33);
} else {
System.out.println(44);
}
}
}
}
}

44

11

33

22

30. Question (1
public class Tester { poi
public static void main(String[] args) { nt)
int k = 1;
switch (k) {
default:
System.out.println("Hello");
case 1:
System.out.println("Welcome");
case 2:
System.out.println("To");
case 3:
System.out.println("Infosys");
break;
}
}
}

Welcome to Infosys

Hello

Welcome

Compilation Error

You might also like