Java Basic MCQ

1
What is Java?

View Answer
Correct Answer: A)

A high-level programming language



Key Explanation


  • Java is a high-level programming language known for its platform independence and object-oriented nature.


Additional Information


  • It was developed by James Gosling at Sun Microsystems and first released in 1995. Java applications run in a virtual machine called the Java Virtual Machine (JVM).


Introduction to Java Programming

What is Java Read More

2
Which of the following is not a primitive data type in Java?

View Answer
Correct Answer: B)

string is not a primitive data type in Java

Additional Information


  • In Java, "string" is not a primitive data type, but a class representing a sequence of characters.


3
What is the purpose of the "public" keyword in Java?

View Answer
Correct Answer: A)

It indicates that a method or variable can be accessed from anywhere

The "public" keyword in Java is used to declare that a class, method, or variable can be accessed from any other class.

4
What does the term "object-oriented" mean in Java programming?

View Answer
Correct Answer: A)

It refers to the ability to create multiple instances of classes



Additional Information


  • Object-oriented in Java means designing programs around objects that have data and methods, promoting code reusability and modularity.



5
Which Java keyword is used to create a new instance of a class?

View Answer
Correct Answer: B)

new keyword is used to create a new instance of a class



Additional Information


  • The "new" keyword in Java is used to create a new object or instance of a class.

6
What does the "static" keyword indicate in Java?

View Answer
Correct Answer: C)

It means the method or variable belongs to the class and not to instances of the class



Key Explanation


  • The "static" keyword in Java denotes that a method or variable belongs to the class itself, not to instances of the class.
  • Learn More about static keyword


7
What is the output of the following code snippet?

int x = 5;
                            int y = 2;
                            int result = x / y;
                            System.out.println(result);
                            

View Answer
Correct Answer: A)

The output will be "3" since the division of 5 by 2 results in an integer value.

8
How do you define a constant variable in Java?

View Answer
Correct Answer: B)

By using the "final" keyword


In Java, constants are defined using the "final" keyword, making their value immutable.

9
What is the correct syntax for a single-line comment in Java?

View Answer
Correct Answer: B)

// This is a comment



Additional Information


  • Single-line comments in Java are written using "//".

10
What is the purpose of the "if" statement in Java?

View Answer
Correct Answer: B

To perform conditional execution



Additional Information


  • The "if" statement in Java is used to perform conditional execution based on a specified condition.





11
Which Java loop is used when the number of iterations is not known beforehand?

View Answer
Correct Answer: A)

while loop is used when the number of iterations is not known beforehand



Additional Information


  • The "while" loop in Java is used when the number of iterations is not known in advance.

12
What does the "break" statement do in Java?

View Answer
Correct Answer: A)

It terminates the current loop and resumes execution after the loop



Key Explanation


  • The "break" statement in Java is used to terminate a loop prematurely.

13
What is the purpose of the "return" keyword in a method in Java?

View Answer
Correct Answer: A)

It returns a value from the method



Key Explanation


  • The "return" keyword in Java is used to return a value from a method back to the caller.

14
How are arrays represented in Java?

View Answer
Correct Answer: A)

arrays represented in Java as [ ]



Additional Information


  • Arrays in Java are represented using square brackets "[]".



15
What is the role of the "extends" keyword in Java class inheritance?

View Answer
Correct Answer: B)

It specifies the superclass that the current class is inheriting from



Additional Information


  • The "extends" keyword in Java is used to establish a subclass that inherits properties and behaviors from a superclass.

16
In Java, which access modifier allows a variable or method to be accessible only within the same package?

View Answer
Correct Answer: C)

package-private access modifier allows a variable or method to be accessible only within the same package



Key Explanation


  • The "package-private" or default access modifier allows variables or methods to be accessed within the same package but not from outside.

17
What does the "try-catch" block do in Java?

View Answer
Correct Answer: A) "try-catch" block handles exceptions in Java



Additional Information


  • The "try-catch" block in Java is used to handle exceptions and prevent program crashes.

18

View Answer
Correct Answer: B)

By implementing the "Runnable" interface or extending the "Thread" class

In Java, you can create a multi-threaded application by implementing the "Runnable" interface or extending the "Thread" class.

19
What is the purpose of the "interface" in Java?

View Answer
Correct Answer: B)

It defines a template for a class to implement



Additional Information


  • Interfaces in Java define a contract for classes to implement specific methods, enabling multiple inheritance and achieving abstraction.

20
What is the correct way to write a for-each loop in Java?

View Answer
Correct Answer: C)

for (int element : array)



Additional Information


  • The correct way to write a for-each loop in Java is by using the syntax "for (int element : array)".





Post your comment