menu

Java Basic Programs for Beginners

Java Basic Programs for Beginners - Important Points


36. Which of the following is a valid way to declare and initialize a character array in Java?

A. char[] arr = {'a', 'b', 'c'};

B. char[] arr = new char[]{'a', 'b', 'c'};

C. char[] arr = new char[3]{'a', 'b', 'c'};

D. char[] arr = new char[3]; arr[0] = 'a'; arr[1] = 'b'; arr[2] = 'c';

Discuss Work Space

Answer: option b

Explanation:

Option B is a valid way to declare and initialize a character array in Java using the new operator.

37.

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

Discuss Work Space

Answer: option b

Explanation:

The / operator performs integer division on the two operands, which is 2 in this case.

38. Which of the following is a valid way to declare and initialize a two-dimensional integer array in Java?

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

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

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

D. int[][] arr = new int[2][3]; arr[0] = {1, 2, 3}; arr[1] = {4, 5, 6};

Discuss Work Space

Answer: option b

Explanation:

Option B is a valid way to declare and initialize a two-dimensional integer array in Java using the new operator.

39.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
String str = "Hello, world!";
System.out.println(str.toUpperCase());
 }
}

A. HELLO, WORLD!

B. Hello, world!

C. hello, world!

D. hELLO, WORLD!

Discuss Work Space

Answer: option a

Explanation:

The toUpperCase method returns a new string in which all the alphabetic characters have been converted to uppercase, which is "HELLO, WORLD!" 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