menu

Java Basic Programs for Beginners

Java Basic Programs for Beginners - Important Points


1.

Which of the following is a valid Java identifier?

A. 1foo

B. _foo

C. fo_o

D. #foo

Discuss Work Space

Answer: option b

Explanation:

Java identifiers can start with a letter, underscore, or currency symbol, and can contain letters, digits, underscores, or currency symbols.


2.

What is the output of the following program?

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

A. 10

B. 12

C. 14

D. 16

Discuss Work Space

Answer: option b

Explanation:

The expression ++x increments the value of x by 1 before using it in the multiplication, so y is assigned the value 6 * 2 = 12.

3. Which of the following is a method of the String class?

A. length()

B. size()

C. count()

D. amount()

Discuss Work Space

Answer: option a

Explanation:

The length() method of the String class returns the number of characters in the string.

4.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
  }
 }
}

A. 1 2 3 4 5

B. 5 4 3 2 1

C. 1 3 5

D. 2 4

Discuss Work Space

Answer: option a

Explanation:

The for loop iterates through the array and prints each element separated by a space.

5. Which of the following operators has the highest precedence in Java?

A. *

B. ++

C. &&

D. +

Discuss Work Space

Answer: option b

Explanation:

The ++ operator has the highest precedence in Java.


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