menu

Java Basics

Java Basics - Important Points


21. Which of the following is a valid way to declare an array in Java?

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

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

C. int numbers = {1, 2, 3};

D. Both A and B

Discuss Work Space

Answer: option d

Explanation:

Both A and B are valid ways to declare an array in Java.

22.

What is the output of the following code?

public class Test {
public static void main(String args[]) {
String name = "Java";
System.out.println(name.substring(1, 3));
 }
}

A. Ja

B. av

C. Jav

D. No output

Discuss Work Space

Answer: option b

Explanation:

The substring() method returns a substring of the original string, starting at index 1 and ending at index 3 (exclusive). In this case, the output is "Ja".


23. Which of the following is NOT a primitive data type in Java?

A. int

B. boolean

C. double

D. string

Discuss Work Space

Answer: option d

Explanation:

String is not a primitive data type in Java.

24.

What is the output of the following code?

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

A. 2

B. 2.5

C. 2

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

When dividing two integers, the result is also an integer. In this case, 5 divided by 2 is 2 with a remainder of 1.


25.

What is the output of the following code?

public class Test {
public static void main(String args[]) {
String name = "Java";
for(int i = 0; i < name.length(); i++) {
System.out.print(name.charAt(i) + " ");
  }
 }
}

A. J a v a

B. Java

C. Jv

D. No output

Discuss Work Space

Answer: option a

Explanation:

The code loops through each character in the string and prints it out with a space. In this case, the output is "J a v a".



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