menu

Java Basic Programs for Beginners

Java Basic Programs for Beginners - Important Points


31.

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.substring(7, 12));
 }
}

A. Hello

B. world

C. Hello,

D. , wor

Discuss Work Space

Answer: option b

Explanation:

The substring method returns the substring of the original string from the start index (inclusive) to the end index (exclusive), which is "world" in this case.

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

A. double var = 3.14159;

B. double var = 3,14159;

C. double var = "3.14159";

D. double var = Double.parseDouble("3.14159");

Discuss Work Space

Answer: option a

Explanation:

Option A is a valid way to declare and initialize a double variable in Java using decimal notation.

33.

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 d

Explanation:

The % operator performs modulo (remainder) division on the two operands, which is 1 in this case.

34. Which of the following is not a valid way to declare and initialize a boolean array in Java?

A. boolean[] arr = {true, false, true};

B. boolean[] arr = new boolean[]{true, false, true};

C. boolean[] arr = new boolean[3] {true, false, true};

D. boolean[] arr = new boolean[3]; arr[0] = true; arr[1] = false; arr[2] = true;

Discuss Work Space

Answer: option c

Explanation:

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

35.

What is the output of the following program?

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

A. 10

B. 11

C. 12

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

The ++ operator increments the value of x by 1, which makes x equal to 11.


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