java interview question and answer | AsgarTech

java interview multiple question and answer | AsgarTech

 

What is Java?

a) A programming language

b) A database management system

c) A hardware component

d) A network protocol

Answer: a) A programming language


What is the difference between a compiler and an interpreter?

a) A compiler translates source code to machine code, while an interpreter executes source code directly

b) A compiler executes source code directly, while an interpreter translates source code to machine code

c) A compiler and an interpreter are the same thing

d) A compiler and an interpreter are both used to debug code

Answer: a) A compiler translates source code to machine code, while an interpreter executes source code directly


What is the keyword used to define a class in Java?

a) class

b) def

c) function

d) struct

Answer: a) class


Which keyword is used to declare a method in Java?

a) function

b) def

c) void

d) method

Answer: c) void


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

a) A method returns a value, while a constructor initializes an object

b) A method initializes an object, while a constructor returns a value

c) A method and a constructor are the same thing

d) A method and a constructor are both used to override code

Answer: a) A method returns a value, while a constructor initializes an object


Which keyword is used to create an instance of a class in Java?

a) create

b) new

c) instance

d) this

Answer: b) new


Which keyword is used to refer to the current object in Java?

a) this

b) that

c) self

d) current

Answer: a) this


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

a) An interface can only contain method declarations, while an abstract class can contain method implementations

b) An abstract class can only contain method declarations, while an interface can contain method implementations

c) An interface and an abstract class are the same thing

d) An interface and an abstract class are both used to define enums

Answer: a) An interface can only contain method declarations, while an abstract class can contain method implementations


What is the access modifier for a class member that is visible only within the same class?

a) public

b) private

c) protected

d) package-private

Answer: b) private


What is the access modifier for a class member that is visible within the same package and all subclasses?

a) public

b) private

c) protected

d) package-private

Answer: c) protected


What is the difference between == and equals() in Java?

a) == compares object references, while equals() compares object values

b) equals() compares object references, while == compares object values

c) == and equals() are the same thing

d) == and equals() are both used to compare strings

Answer: a) == compares object references, while equals() compares object values


What is the purpose of the final keyword in Java?

a) To indicate that a class cannot be inherited

b) To indicate that a method cannot be overridden

c) To indicate that a variable cannot be reassigned

d) All

No comments: