Java interview question – Quiz 4
Java interview question
Do you possess the necessary skills to ace a Java interview question We are available to assist you with solidifying your Java skills and ideas. Let’s have a brief understanding of Java before we get started.
What is Java?
The high-level programming language Java was created in 1982 by James Gosling. It may be used to create extensive applications and is founded on the ideas of object-oriented programming.
All the frequently asked Java coding interview questions as well as the most popular Core Java, String Handling, Java 8 and multithreading interview questions, Java OOPs, Java exception handling, and collections interview questions are covered in the article that follows.
Java interview question
Java Coding Questions
Question 1: What is the output of the following Java code?
public class Main { public static void main(String[] args) { int x = 5; System.out.println(x++); } }
- 5
- 6
- 0
- 1
Question 2: Which access modifier is not allowed for top-level classes in Java?
- public
- private
- protected
- default (package-private)
Question 3: What is the keyword used to define a constant in Java?
- static
- final
- constant
- public
Question 4: What is the purpose of the `break` statement in a Java `switch` statement?
- To exit the switch statement and continue with the next case.
- To skip the current case and continue with the default case.
- To exit the program entirely.
- To restart the switch statement from the beginning.
Question 5: Which Java keyword is used to create a new instance of a class?
- new
- class
- instance
- object
Question 6: What is the purpose of the `super` keyword in Java?
- To call a superclass constructor and access superclass methods or variables.
- To access the current class’s constructor.
- To invoke a static method.
- To break out of a loop or switch statement.
Question 7: What is the difference between `==` and `.equals()` when comparing strings in Java?
- They are equivalent and can be used interchangeably.
- `==` compares object references, while `.equals()` compares string contents.
- `==` compares string contents, while `.equals()` compares object references.
- Both always return `true` for any strings.
Question 8: What is the purpose of the `try`, `catch`, and `finally` blocks in Java for exception handling?
- `try` is used to throw an exception, `catch` is used to handle the exception, and `finally` is used for cleanup code.
- `try` is used to handle an exception, `catch` is used to throw the exception, and `finally` is used for cleanup code.
- `try` is used to define a block of code to be tested for exceptions, `catch` is used to handle the exception, and `finally` is used for cleanup code.
- `try` is used to define a block of code to be tested for exceptions, and `catch` is used to define the exception type.
Question 9: What is method overloading in Java?
- Method overloading is the process of creating a new method with the same name but different parameter types or a different number of parameters within the same class.
- Method overloading is the process of creating multiple methods with different names but the same parameter types.
- Method overloading is the process of creating a new method with a different return type in the same class.
- Method overloading is the process of creating multiple methods with the same name and the same parameter types.
Question 10: What is the purpose of the `this` keyword in Java?
- `this` is used to create a new object of a class.
- `this` is used to refer to the current instance of a class.
- `this` is used to invoke a method in another class.
- `this` is used to create a static method in a class.
Question 11: What is the difference between `ArrayList` and `LinkedList` in Java?
- `ArrayList` is a synchronized collection, while `LinkedList` is not.
- `ArrayList` is faster for random access, while `LinkedList` is faster for inserting and deleting elements.
- `ArrayList` can only store integers, while `LinkedList` can store any data type.
- `ArrayList` and `LinkedList` are the same, just with different names.
Question 12: What is the purpose of the `static` keyword in Java?
- `static` is used to create an instance of a class.
- `static` is used to define a constant value.
- `static` is used to create a subclass of a class.
- `static` is used to define a class-level variable or method that can be accessed without creating an instance of the class.
Question 13: What is the purpose of the `extends` keyword in Java for classes?
- `extends` is used to create a new class.
- `extends` is used to implement an interface.
- `extends` is used to define a superclass-subclass relationship, allowing inheritance of fields and methods.
- `extends` is used to create a copy of an existing class.
Question 14: What is a constructor in Java?
- A constructor is a method used for creating new objects.
- A constructor is used to destroy objects.
- A constructor is a special type of method used for subclassing.
- Java does not support constructors.
Question 15: What is the purpose of the `public` access modifier in Java?
- `public` allows access to the element from within the same class only.
- `public` restricts access to the element from all other classes.
- `public` allows access to the element from any class or package.
- `public` is not a valid access modifier in Java.
Question 16: What is the purpose of the `break` statement in a loop in Java?
- `break` is used to exit the loop and continue with the next iteration.
- `break` is used to skip the current iteration and continue with the next one.
- `break` is used to create an infinite loop.
- `break` is used to restart the loop from the beginning.
Question 17: What is method overriding in Java?
- Method overriding is the process of creating multiple methods with the same name but different parameter types in the same class.
- Method overriding is the process of creating a new method with the same name but different parameter types in a subclass.
- Method overriding is the process of creating a new method with a different return type in a subclass.
- Method overriding is not allowed in Java.
Question 18: What is the purpose of the `final` keyword in Java?
- `final` is used to define a class that cannot be extended.
- `final` is used to prevent the modification of variables, methods, or classes.
- `final` is used to make a class abstract.
- `final` is used to define a class that can only be extended.
Question 19: What is the purpose of the `throws` keyword in Java?
- `throws` is used to define custom exceptions.
- `throws` is used to declare that a method may throw exceptions and specify the exception types that the method might throw.
- `throws` is used to catch exceptions and handle them within a method.
- `throws` is used to terminate a program.
Question 20: What is the purpose of the `new` keyword in Java?
- `new` is used to create a new object of a class.
- `new` is used to create a new class.
- `new` is used to define a variable.
- `new` is not a valid keyword in Java.
Java interview question
Youtube :- QR Code Generator With HTML, CSS, and JavaScript ( Free Source Code)
link:- https://youtu.be/Z_F_OUtsNBo
Post Comment