menu

Java Basic Programs for Beginners

Java Basic Programs for Beginners - Important Points


26. Which of the following is a valid way to declare a constant in Java?

A. final int var = 5;

B. int const var = 5;

C. const int var = 5;

D. int var = 5; final

Discuss Work Space

Answer: option a

Explanation:

Option A is a valid way to declare a constant in Java using the final keyword.

27.

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.5

B. 2

C. 3

D. 5

Discuss Work Space

Answer: option b

Explanation:

The / operator performs integer division when both operands are integers, so the result is 2 in this case.

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

A. char var = 'a';

B. char var = "a";

C. char var = 97;

D. char var = 'a'.toUpperCase();

Discuss Work Space

Answer: option a

Explanation:

Option A is a valid way to declare and initialize a char variable in Java using single quotes.

29.

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. 1

B. 0

C. Compile error

D. Runtime error

Discuss Work Space

Answer: option b

Explanation:

The expression x == y compares the values of x and y for equality, which is false in this case.

30. Which of the following is not a valid way to declare and initialize an integer array in Java?

A. int[] arr = {1, 2, 3};

B. int[] arr = new int[]{1, 2, 3};

C. int[] arr = new int[3] {1, 2, 3};

D. int[] arr = new int[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3;

Discuss Work Space

Answer: option c

Explanation:

Option C is not a valid way to declare and initialize an integer array in JavWhen using the new operator to create an array, the size of the array must be specified without initializing the elements.


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