0% found this document useful (0 votes)
24 views4 pages

Exam 2-1-25

The document contains a series of multiple-choice questions related to Java programming, covering topics such as output of code snippets, error identification, and understanding of Java syntax and semantics. Each question presents a Java code snippet or expression, followed by four answer options. The questions test knowledge of Java concepts including variable scope, data types, operators, and control structures.
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)
24 views4 pages

Exam 2-1-25

The document contains a series of multiple-choice questions related to Java programming, covering topics such as output of code snippets, error identification, and understanding of Java syntax and semantics. Each question presents a Java code snippet or expression, followed by four answer options. The questions test knowledge of Java concepts including variable scope, data types, operators, and control structures.
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/ 4

1. What will be the output of the following Java program?

class A {
public static void main(String args[]) {
int g = 3;
System.out.print(++g * 8);
}
}
A. 32 B. 33 C. 24 D. 25

2. What will be the output of the following Java program?


class A {
public static void main(String args[]) {
double a, b,c;
a = 3.0/0;
b = 0/4.0;
c=0/0.0;

System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
A. NaN B. Infinity C. 0.0 D. all of the mentioned

3. What will be the output of the following Java program?


class A {
public static void main(String args[]) {
int x;
x = 5;
{
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}
Compilation error B. Runtime error C. 5 6 5 6 D. 5 6 5

4. What will be the error in the following Java code?


byte b = 50;
b = b * 50;
a) b cannot contain value 50
b) b cannot contain value 100, limited by its range
c) No error in this code
d) * operator has converted b * 50 into int, which cannot be converted to byte without casting

5. What will be the output of the following Java program?


class A{
public static void main(String args[]) {
byte x = 64;
int i;
byte y;
i = x << 2;
y = (byte) (x << 2);
System.out.print(i + " " + y);
1
}
}
a) 0 256 b) 0 64 c) 256 0 d) 64 0

6. What will be the output of the following Java code snippet?


class A {
public static void main(String args[]) {
if(args.length>0)
System.out.println(args.length);
}
}
a) The snippet compiles and runs but does not print anything
b) The snippet compiles, runs and prints 0
c) The snippet compiles, runs and prints 1
d) The snippet does not compile

7. What will be the output of the following Java code snippet?


class recursion {
int func (int n) {
int result;
if (n == 1)
return 1;
result = func (n - 1);
return result;
}
}
}
class A {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.func(5));
}
}
a) 1 b) 120 c) 0 ) None of the mentioned

8. What will be the output of the following Java code snippet?


class A {
public static void main(String args[]) {
String c = "Hello i love java";
boolean var;
var = c.startsWith("hello");
System.out.println(var);
}
}
a) 0 b) true c) 1 d) false

9. What will be the output of the following Java code?


class A {
public static void main(String args[]) {
double x = 3.14;
int y = (int) Math.ceil(x);
System.out.print(y);
}
}
a) 3 b) 0 c) 4 d) 3.0
2
10. Evaluate the following Java expression, if x=3, y=5, and z=10:
++z + y - y + z + x++
A. 24 B. 23 C. 20 D. 25

11. Which of the following for loop declaration is not valid?


A. for ( int i = 99; i >= 0; i / 9 ) B. for ( int i = 7; i <= 77; i += 7 )
C. for ( int i = 20; i >= 2; - -i ) D. for ( int i = 2; i <= 20; i = 2* i )

12. What will be the output of the following Java code?


public class A{
public static void main(String[] args) {
int value = 3, sum = 6 + -- value;

int data = --value + ++value / sum++ * value++ + ++sum % value--;


System.out.println(data);
}
}
a) 1 b) 2 c) 0 d) 3

13. What will be the output of the following Java code?


public class A{
public static void main(String[] args) {
int temp = 9;
int data = 8;
System.out.println(temp & data);
}
}
a) 9 b) 8 c) 1000 d) 1001

14. What will be the output of the following Java code?


class A {
public static void main(String[] args) {
for(int i = 0; true; i++) {
System.out.println("Hello");
break;
}
}
}
a. Hello b. Runtime error c. Compilation error d. Infinite time print Hello

15. What will be the output of the following Java code?


public class A{
public static void main(){
int x = 5;
int y = 10;
System.out.println(x + y + "Hello");
}
}
a) 15Hello b) Hello15 c) 510Hello d) 5Hello10

16. What will be the output of the following Java code?


public class A{
public static void main(String []args){
String str1 = "Hello";
String str2 = new String("Hello");
3
System.out.println(str1 == str2);
}
}
a) true b) false c) It will cause a compilation error d) It will throw a runtime exception.

17. What will be the output of the following Java program?


class A {
public static void main(String args[]){
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
a) 1 b) 2 c) 3 d) 4

18. What will be the output of the following Java program?


class A {
public static void main(String args[]) {
int x = 2;
int y = 0;
for ( ; y < 10; ++y)
{
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}
a) 1 3 5 7 b) 2 4 6 8 c) 1 3 5 7 9 d) 1 2 3 4 5 6 7 8 9

19. What will be the output of the following Java program?


public class A {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
System.out.println(i + ' ');
}
}
}
a. 32 33 34 35 36 b. 0 1 2 3 4

20. Which of the following does not compile?


A. int num = 999; B. int num = 9_9_9; C. int num = _9_99; D. None of the above; they all compile

You might also like