java interview objective question answer | AsgarTech

 java interview objective question answer

What is Java?

a) A scripting language
b) A markup language
c) A programming language
d) A markup and programming language

Answer: c) A programming language

What is the difference between a class and an object in Java?

a) A class is a blueprint for objects, while an object is an instance of a class.
b) A class and an object are the same thing in Java.
c) A class is a collection of methods, while an object is a collection of variables.
d) A class is a collection of variables, while an object is a collection of methods.

Answer: a) A class is a blueprint for objects, while an object is an instance of a class.

What is the difference between a constructor and a method in Java?

a) A constructor is used to initialize an object, while a method is used to perform some action.
b) A constructor is used to perform some action, while a method is used to initialize an object.
c) A constructor and a method are the same thing in Java.
d) A constructor and a method can both be used to initialize an object.

Answer: a) A constructor is used to initialize an object, while a method is used to perform some action.

What is the difference between an abstract class and an interface in Java?

a) An abstract class can be instantiated, while an interface cannot.
b) An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods.
c) An abstract class can be used to implement multiple interfaces, while an interface cannot.
d) An abstract class and an interface are the same thing in Java.

Answer: b) An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods.

What is the difference between a static method and a non-static method in Java?

a) A static method can be called without creating an object, while a non-static method can only be called on an object.
b) A non-static method can be called without creating an object, while a static method can only be called on an object.
c) A static method and a non-static method are the same thing in Java.
d) A static method and a non-static method can both be called without creating an object.

Answer: a) A static method can be called without creating an object, while a non-static method can only be called on an object.

What is the difference between inheritance and composition in Java?

a) Inheritance is a way to reuse code by creating a new class from an existing one, while composition is a way to reuse code by including an object of one class inside another class.
b) Inheritance and composition are the same thing in Java.
c) Inheritance and composition both involve creating new classes.
d) Inheritance is a way to reuse code by including an object of one class inside another class, while composition is a way to reuse code by creating a new class from an existing one.

Answer: a) Inheritance is a way to reuse code by creating a new class from an existing one, while composition is a way to reuse code by including an object of one class inside another class.

What is the difference between a while loop and a do-while loop in Java?

a) A while loop checks the condition at the end of the loop, while a do-while loop checks the condition at the beginning of the loop.
b) A while loop checks the condition at the beginning of the loop, while a do-while loop checks the condition at the end of the loop.
c) A while loop and a


here are 20 more short Java interview questions:


What is a constructor in Java?

Answer: A constructor is a special method used to initialize an object of a class.

What is the difference between a stack and a queue in Java?

Answer: A stack is a Last-In-First-Out (LIFO) data structure, while a queue is a First-In-First-Out (FIFO) data structure.

What is the purpose of the 'this' keyword in Java?

Answer: The 'this' keyword refers to the current object instance in Java.

What is an interface in Java?

Answer: An interface is a collection of abstract methods that can be implemented by any class.

What is the difference between checked and unchecked exceptions in Java?

Answer: Checked exceptions are checked at compile-time, while unchecked exceptions are not.

What is the difference between final, finally, and finalize in Java?

Answer: 'final' is a keyword used to make a variable constant, 'finally' is a block used to execute code after a try-catch block, and 'finalize' is a method used to perform cleanup before an object is garbage collected.

What is the difference between a HashSet and a TreeSet in Java?

Answer: A HashSet is an unordered collection of unique elements, while a TreeSet is an ordered collection of unique elements.

What is polymorphism in Java?

Answer:Polymorphism is the ability of an object to take on many forms.

What is the difference between public, private, and protected in Java?

Answer:'public' means that the variable or method can be accessed from anywhere, 'private' means that the variable or method can only be accessed within the same class, and 'protected' means that the variable or method can be accessed within the same class or any subclass.

What is a package in Java?

Answer:A package is a collection of related classes and interfaces.

What is the difference between an array and an ArrayList in Java?

Answer: An array is a fixed-length data structure, while an ArrayList is a dynamic data structure that can change in size.

What is the difference between a String and a StringBuilder in Java?

Answer: A String is an immutable sequence of characters, while a StringBuilder is a mutable sequence of characters.

What is a static variable in Java?

Answer: A static variable is a variable that belongs to the class and not to the object instance.

What is method overloading in Java?

Answer: Method overloading is the ability to define multiple methods with the same name but different parameters in a class.

What is method overriding in Java?

Answer: Method overriding is the ability to provide a different implementation of a method in a subclass than that provided by the superclass.

What is the difference between a HashMap and a TreeMap in Java?

Answer: A HashMap is an unordered collection of key-value pairs, while a TreeMap is an ordered collection of key-value pairs.

What is the difference between a synchronized method and a synchronized block in Java?

Answer:A synchronized method locks the entire object, while a synchronized block locks only a specific block of code.

What is the difference between composition and aggregation in Java?

Answer: Composition is a strong form of aggregation where the containing object is responsible for the existence and lifetime of the contained object, while aggregation is a weaker form of composition where the containing object has a reference to the contained object, but is not responsible for its lifetime.

What is the difference between an abstract class and an interface in Java?

Answer:An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods.



No comments: