Java is a versatile programming language used for developing a wide range of applications. It offers strong support for object-oriented programming and is known for its robustness and scalability. Java is widely employed by companies across industries.
Last updated on:- 8th June 2023
1.
What is method overloading in Java ?
Defining multiple methods with the same name but different parameters
Defining multiple methods with the same name and same parameters
Defining multiple methods with different return types
Defining multiple methods with different access modifiers
Method overloading in Java refers to defining multiple methods with the same name but different parameters. This allows methods to be invoked with different arguments, providing flexibility and code reuse. The compiler determines the appropriate method to invoke based on the number, types, and order of the arguments.
2.
What is the purpose of the 'public' access modifier in Java?
To define a constant variable
To perform arithmetic operations on floating-point numbers
To specify that a method or variable can be accessed from any class
To restrict access to a variable or method
The 'public' access modifier in Java is used to specify that a method or variable can be accessed from any class. It provides the highest level of accessibility, allowing the method or variable to be accessed from any other class or package.
3.
What is a static method in Java ?
A method that can be accessed without creating an instance of the class
A method that can only be accessed by other static methods
A method that is automatically invoked when an object is created
A method that is used to access private class members
A static method in Java is a method that can be accessed without creating an instance of the class. It belongs to the class itself rather than an instance of the class. Static methods are commonly used for utility functions or operations that do not depend on specific object state.
4.
What is method overriding in Java ?
Defining multiple methods with the same name but different parameters
Defining multiple methods with the same name and same parameters
Defining multiple methods with different return types
Defining a method in a subclass that is already defined in its superclass
Method overriding in Java refers to defining a method in a subclass that is already defined in its superclass. The method in the subclass provides a specific implementation for the method defined in the superclass. This allows for polymorphic behavior, where the appropriate method is invoked based on the actual object type.
5.
What is the 'final' keyword used for in Java ?
To define a constant variable
To specify the starting point of program execution
To restrict access to a variable or method
To indicate the end of a loop
The 'final' keyword in Java is used to define a constant variable. Once a variable is declared as 'final', its value cannot be changed. It is commonly used to define constants that should not be modified during program execution.
6.
What is the purpose of the 'try-catch' block in Java ?
To define a constant variable
To perform arithmetic operations on floating-point numbers
To handle and recover from exceptions
To restrict access to a variable or method
The 'try-catch' block in Java is used to handle and recover from exceptions. Code that may potentially throw an exception is placed within the 'try' block, and if an exception occurs, it is caught and handled by the corresponding 'catch' block. This allows for graceful error handling and prevents program termination.
7.
What is the purpose of the 'super' keyword in Java ?
To define a constant variable
To perform arithmetic operations on floating-point numbers
To restrict access to a variable or method
To refer to the superclass or parent class
The 'super' keyword in Java is used to refer to the superclass or parent class. It is commonly used to access superclass members, invoke superclass constructors, or override superclass methods. It allows for code reuse and provides a way to access and modify inherited members.
8.
What is the purpose of the 'private' access modifier in Java ?
To define a constant variable
To perform arithmetic operations on floating-point numbers
To specify that a method or variable can be accessed only within the same class
To restrict access to a variable or method
The 'private' access modifier in Java is used to specify that a method or variable can be accessed only within the same class. It provides the highest level of encapsulation, restricting access to the method or variable to only the class in which it is declared.
9.
What is the correct way to define a constant variable in Java ?
final int x = 5;
const int x = 5;
static final int x = 5;
final static int x = 5;
In Java, constants are typically defined using the 'final' keyword. The correct syntax for defining a constant variable is 'final int x = 5;', where 'x' is the variable name and '5' is the constant value.
10.
What is a core feature of Java ?
Portability
Speed
Low-level programming
Memory management
Java is known for its portability, which means that Java programs can run on any platform that has a Java Virtual Machine (JVM) installed.
11.
Which keyword is used to create an object in Java ?
create
new
instance
object
The 'new' keyword is used to create an object in Java. It is followed by the constructor of the class that is being instantiated. For example, 'ClassName objectName = new ClassName();' creates a new object of type 'ClassName'.
12.
What is the purpose of the 'this' keyword in Java ?
To refer to the current object
To create an instance of a class
To define a constant variable
To restrict access to a variable or method
The 'this' keyword in Java is used to refer to the current object. It can be used to access instance variables and methods within the class. It is often used to disambiguate between instance variables and parameters with the same name.
13.
What is the purpose of the 'abstract' keyword in Java ?
To restrict access to a variable or method
To define a constant variable
To create a class-level variable or method
To define a class that cannot be instantiated directly
The 'abstract' keyword in Java is used to define a class that cannot be instantiated directly. It serves as a blueprint for creating derived classes and must be extended by a subclass to be used. Abstract classes can contain abstract methods that must be implemented by the subclasses.
14.
What is the purpose of the 'instanceof' operator in Java ?
To restrict access to a variable or method
To create a class-level variable or method
To define a constant variable
To check the type of an object
The 'instanceof' operator in Java is used to check the type of an object. It returns 'true' if the object is an instance of the specified type or a subclass of the specified type, and 'false' otherwise. It is commonly used in conditional statements and type checking.
15.
Which keyword is used to define a method in Java ?
function
def
method
void
The 'void' keyword is used to define a method in Java. It specifies that the method does not return any value. The method definition consists of the return type, followed by the method name, parameters (if any), and the method body enclosed in curly braces.
16.
What is the purpose of the 'return' statement in a method ?
To terminate the execution of the method
To specify the return type of the method
To define the parameters of the method
To declare a local variable
The 'return' statement is used to terminate the execution of a method and return a value (if the method has a return type). It can also be used to return control to the caller before the end of the method is reached. Once the 'return' statement is encountered, the method execution is immediately stopped.
17.
What is the syntax for creating an object of a class in Java ?
new Object = MyClass();
MyClass myObject = new MyClass();
MyClass = new Object();
MyClass myObject = MyClass();
To create an object of a class in Java, you use the 'new' keyword followed by the class name and parentheses. This is followed by the assignment operator '=' and the keyword 'new' again, along with the class name and parentheses. Finally, you end the statement with a semicolon. The correct syntax is 'MyClass myObject = new MyClass();'.
18.
What is the default value of an uninitialized variable of type int in Java ?
0
1
null
false
The default value of an uninitialized variable of type int in Java is 0. When a variable is declared but not explicitly initialized, it is assigned its default value based on its data type. For integer types like int, the default value is 0.
19.
Which of the following is true about Java exceptions ?
Exceptions are used for normal program flow
All exceptions in Java are checked exceptions
An exception must always be caught and handled by the programmer
Exceptions can be used to handle unexpected errors and exceptional conditions
In Java, exceptions are used to handle unexpected errors and exceptional conditions that can occur during the execution of a program. Exceptions allow for the separation of normal program flow from error handling. While checked exceptions must be declared and handled, there are also unchecked exceptions that do not require explicit handling.
20.
Which of the following is not a valid access modifier in Java ?
public
private
protected
internal
The 'internal' keyword is not a valid access modifier in Java. The correct access modifiers in Java are 'public', 'private', and 'protected'. The 'internal' keyword is specific to other programming languages like C# and is not recognized in Java.
21.
What is a Java package ?
A collection of classes and interfaces
A software development kit
A version control system
A database management system
A Java package is a way to organize classes and interfaces into a hierarchical structure. It provides a namespace for the classes, preventing naming conflicts. Packages help in modularizing code, improving code reusability, and facilitating the management of large codebases.
22.
What is a Java constructor ?
A method that is automatically called when an object is created
A method that is used to deallocate resources
A method that is used to handle exceptions
A method that is used to define constants
A Java constructor is a special method that is automatically called when an object is created. It is used to initialize the object and set its initial state. Constructors have the same name as the class and do not have a return type. They can take parameters to initialize the object with specific values.
23.
What is method chaining in Java ?
Calling multiple methods on an object in a single line by returning the object itself from each method
Calling static methods on a class
Calling methods on a superclass
Calling methods with the same name but different parameters
Method chaining in Java refers to calling multiple methods on an object in a single line by returning the object itself from each method. This is achieved by having each method return the current object ('this') after performing its operations. Method chaining can result in more concise and readable code.
24.
Which of the following is true about interfaces in Java?
Interfaces can be instantiated using the 'new' keyword
A class can implement multiple interfaces
Interfaces can have constructors
An interface can extend multiple interfaces
In Java, a class can implement multiple interfaces. This allows a class to inherit behavior from multiple sources by implementing the methods defined in the interfaces it implements. However, interfaces themselves cannot be instantiated using the 'new' keyword, and they cannot have constructors. Interfaces can extend other interfaces, but a class can implement multiple interfaces regardless of whether they are related through inheritance.
25.
What is a Java interface ?
A collection of classes and interfaces
A software development methodology
A version control system
A blueprint of a class that defines a set of methods that implementing classes must implement
A Java interface is a blueprint of a class that defines a set of methods that implementing classes must implement. An interface can also include constant variables. By implementing an interface, a class guarantees that it will provide the defined methods. Interfaces support the concept of multiple inheritance in Java.
26.
What is the difference between a class and an interface in Java ?
A class can have both method definitions and method implementations, while an interface can only have method definitions.
A class can have multiple superclasses, while an interface can only extend one interface.
A class can be instantiated to create objects, while an interface cannot be instantiated.
A class can implement multiple interfaces, while an interface can only extend one interface.
In Java, a class is a blueprint for creating objects. It can have both method definitions and method implementations. A class can be instantiated using the 'new' keyword to create objects. Whereas, an interface is a blueprint of a class that defines a set of methods that implementing classes must implement. An interface can only have method definitions, and it cannot be instantiated. A class can implement multiple interfaces, but it can only extend one superclass.
27.
What is the Java Virtual Machine (JVM)?
A compiler that translates Java source code into bytecode
A runtime environment that executes Java bytecode
A development tool for debugging Java programs
A database management system for Java applications
The Java Virtual Machine (JVM) is a runtime environment that executes Java bytecode. It provides the necessary infrastructure to run Java programs on different platforms without requiring recompilation. The JVM handles memory management, bytecode interpretation, and other runtime tasks.
28.
What is the difference between the Java Virtual Machine (JVM) and the Java Development Kit (JDK)?
The JVM is used to compile Java source code, while the JDK is used to execute Java bytecode.
The JVM is a runtime environment that executes Java bytecode, while the JDK is a software development kit that includes the tools needed to develop and run Java applications.
The JVM is a development tool for debugging Java programs, while the JDK is a runtime environment that executes Java bytecode.
The JVM is a database management system for Java applications, while the JDK is a software development methodology.
The Java Virtual Machine (JVM) is a runtime environment that executes Java bytecode. It provides the necessary infrastructure to run Java programs on different platforms without requiring recompilation. Whereas, the Java Development Kit (JDK) is a software development kit that includes the tools needed to develop, compile, and run Java applications. The JDK includes the JVM, as well as compilers, debuggers, and other development tools.
29.
What is the purpose of the 'if' statement in Java?
To declare a loop
To perform type casting
To control the flow of execution based on a condition
To define a method
The 'if' statement in Java is used to control the flow of execution based on a condition. It allows a block of code to be executed only if a certain condition is true. If the condition is false, the code block is skipped.
30.
What is a variable in Java?
A reserved word in Java
A named memory location used to store data
A data type in Java
A class in Java
In Java, a variable is a named memory location used to store data. It is used to hold values that can be accessed and manipulated in a program. Variables are declared with a specific data type and can be assigned different values during the execution of the program.