menu

Java Basic Programs for Beginners

Java Basic Programs for Beginners - Important Points


21.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
int x = 5;
int y = 2;
System.out.println(x + y);
 }
}

A. 2

B. 3

C. 5

D. 7

Discuss Work Space

Answer: option d

Explanation:

The + operator performs addition on the two operands, which is 7 in this case.

22. Which of the following is a valid way to declare and initialize a boolean variable in Java?

A. boolean var = "true";

B. boolean var = 1;

C. boolean var = true;

D. boolean var = True;

Discuss Work Space

Answer: option c

Explanation:

Option C is a valid way to declare and initialize a boolean variable in Java.

23.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
int x = 5;
int y = 2;
System.out.println(x * y);
 }
}

A. 2

B. 3

C. 5

D. 10

Discuss Work Space

Answer: option d

Explanation:

The * operator performs multiplication on the two operands, which is 10 in this case.

24. Which of the following is not a valid way to declare and initialize a String variable in Java?

A. String var = "hello";

B. String var = new String("hello");

C. String var = 'hello';

D. String var = "hello".toUpperCase();

Discuss Work Space

Answer: option c

Explanation:

Option C is not a valid way to declare and initialize a String variable in JavStrings are enclosed in double quotes, not single quotes.

25.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
int x = 5;
int y = 2;
System.out.println(x - y);
 }
}

A. 2

B. 3

C. 5

D. -3

Discuss Work Space

Answer: option c

Explanation:

The - operator performs subtraction on the two operands, which is 3 in this case.


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now