0% found this document useful (0 votes)
8 views14 pages

Java Core MCQ

This document is a Java test consisting of multiple-choice questions covering various topics such as bytecode conversion, operators, data types, loops, arrays, strings, object-oriented principles, exceptions, threads, and collections. The test is designed to assess knowledge of Java programming concepts and syntax. It includes questions on fundamental aspects of Java, such as encapsulation, polymorphism, and the use of interfaces.

Uploaded by

hieupb68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Java Core MCQ

This document is a Java test consisting of multiple-choice questions covering various topics such as bytecode conversion, operators, data types, loops, arrays, strings, object-oriented principles, exceptions, threads, and collections. The test is designed to assess knowledge of Java programming concepts and syntax. It includes questions on fundamental aspects of Java, such as encapsulation, polymorphism, and the use of interfaces.

Uploaded by

hieupb68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Test

Name:.....................................................
Time: 90 minutes

Which of the following is responsible for converting bytecode into machine


code?
a) JDK
b) JRE
c) JVM
d) Java Compiler

What does the == operator compare in Java objects?


a) Values
b) References
c) Hash codes
d) Fields

Which operator is used for logical "AND" operation?


a) &&
b) &
c) ||
d) |

Which operator has the highest precedence?


a) +
b) *
c) ()
d) &&
What is the output of the expression true || false?
a) true
b) false
c) null
d) 0

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


a) byte
b) String
c) double
d) short

What is the default value of the int data type?


a) 0
b) 1
c) null
d) Undefined

Which of the following data types can store a floating-point number?


a) int
b) byte
c) double
d) char

Which data type can store a single character?


a) String
b) byte
c) char
d) int
What's the main difference between int and Integer in Java?
a) No difference
b) Integer can store larger values than int
c) int is a primitive data type, while Integer is a class
d) int can be null, while Integer cannot

Which loop construct in Java is best suited when the number of iterations
is known?
a) for loop
b) while loop
c) do-while loop
d) break statement

What is the purpose of the continue statement in a loop?


a) To exit the loop immediately
b) To skip the current iteration and move to the next iteration
c) To terminate the program
d) To execute a specific block of code

What is the key difference between a while loop and a do-while loop in
Java?
a) The syntax used to define the loop
b) The number of iterations performed
c) The condition check timing
d) The ability to use the break statement

Which statement is used to exit a loop prematurely?


a) return statement
b) continue statement
c) break statement
d) exit statement
Which loop construct is best suited for iterating over an array or a
collection?
a) for loop
b) while loop
c) do-while loop
d) continue statement

How do you declare an array in Java?


a) int arrayName;
b) int[] arrayName;
c) int arrayName[];
d) Both b and c

How do you initialize an array in Java?


a) int[] arr = (1,2,3,4,5);
b) int arr = {1,2,3,4,5};
c) int[] arr = new int[]{1,2,3,4,5};
d) int arr = new int(5);

What happens when you try to access an array element with an index that
is out of bounds?
a) It returns -1
b) It returns a null value
c) It throws an exception
d) It initializes a new array

How can you check if two arrays are equal in Java?


a) Use the == operator
b) Use the .equals() method
c) Use [Link]() method
d) Compare each element one by one
How do you access the fourth element of an array named numbers?
a) numbers[4];
b) numbers(3);
c) numbers[3];
d) [Link](3);

Which of the following operators is used for concatenation of two strings?


a) +
b) *
c) &
d) +=

In Java, strings are:


a) Primitive data types
b) Immutable objects
c) Mutable objects
d) Arrays of characters

How do you find the length of a string named 'example'?


a) [Link]()
b) [Link]()
c) [Link]()
d) [Link]()

What is the result of the expression "Java" + "Programming"?


a) JavaProgramming
b) Java Programming
c) Java-Programming
d) JavaProgramming-
Which method is used to compare two strings for equality?
a) ==
b) equals()
c) compare()
d) isEqual()

What is the root class for all Java classes?


a) Object
b) Class
c) Superclass
d) Root

What is encapsulation in Java?


a) The process of combining data and methods into a single unit
b) The process of hiding data and methods within a class
c) The process of creating multiple instances of a class
d) The process of reusing code from existing classes

What is polymorphism in Java?


a) The ability of a class to inherit properties and behaviors from another class
b) The process of hiding data and methods within a class
c) The process of creating multiple instances of a class
d) The ability of an object to take on many forms

What are abstract classes in Java?


a) Classes that cannot be instantiated
b) Classes that can be used as blueprints for creating objects
c) Classes that only contain abstract methods
d) All of the above
What is the purpose of the "super" keyword in Java?
a) To refer to the current object
b) To invoke the superclass constructor or methods
c) To create multiple instances of a class
d) To hide data and methods within a class

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


a) To refer to the superclass
b) To create multiple instances of a class
c) To hide data and methods within a class
d) To refer to the current object

What is the purpose of the "final" keyword in Java?


a) To prevent the inheritance of a class
b) To prevent overriding of a method
c) To prevent modification of a variable's value
d) All of the above

What is an interface in Java?


a) A class
b) A data type
c) A blueprint for a class
d) A data structure

Which keyword is used to implement an interface?


a) extends
b) new
c) interface
d) implements
Can an interface extend another interface in Java?
a) No
b) Yes

Which of the following access modifiers are implicitly applied to variables


in an interface?
a) private
b) protected
c) public
d) default

Is it possible to create an instance of an interface?


a) Yes
b) No

How many interfaces can a Java class implement?


a) None
b) Only one
c) Two
d) As many as needed

Can an interface inherit from a class?


a) Yes
b) No

Which keyword is used to define a default method in an interface?


a) static
b) default
c) final
d) abstract
Are all methods in an interface abstract?
a) Yes
b) No

Which Java feature helps achieve multiple inheritance?


a) Abstract classes
b) Static methods
c) Interfaces
d) Enums

What is the purpose of the instanceof operator?


a) Multiply instances
b) Compare two object references
c) Check if an object is an instance of a specific class or interface
d) To create a new instance of a class

Which keyword is used to declare a class variable?


a) volatile
b) transient
c) static
d) final

Which keyword is used to prevent a class from being inherited?


a) final
b) abstract
c) class
d) extends

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


a) new
b) return
c) this
d) create

Which keyword is used to inherit the properties and methods from another
class?
a) import
b) package
c) extends
d) implements

Which of these is a checked exception?


a) NullPointerException
b) ArithmeticException
c) IOException
d) IndexOutOfBoundsException

Which of the following can be used to create a custom checked exception?


a) Extending the Exception class
b) Extending the Error class
c) Extending the RuntimeException class
d) None of the above

Which keyword is used to manually throw an exception in Java?


a) new
b) throw
c) throws
d) Throwable
Which of these classes is the superclass of all Exception and Error
classes?
a) Exception
b) Error
c) Throwable
d) Object

What does the finally block do?


a) Catches any exception
b) Executes whether an exception is thrown or not
c) Executes only when an exception is thrown
d) Executes only when an exception is not thrown

Which keyword in Java is used for constant variables?


a) const
b) static
c) constant
d) final

In Java, what is the primary purpose of the Thread class?


a) File handling
b) String operations
c) Network operations
d) Creating and executing threads

Which method is used to start the execution of a thread?


a) run()
b) start()
c) execute()
d) go()
What is a daemon thread in Java?
a) A thread that monitors and logs other threads
b) A thread that runs continuously in the background
c) A thread that manages memory usage
d) A thread that is part of the Java virtual machine

Which interface represents a collection of objects in which duplicate values


can be stored?
a) List
b) Set
c) Map
d) ListMap

What will be the initial capacity of an ArrayList if it is created with the no-
argument constructor?
a) 0
b) 5
c) 10
d) 16

What does a Set guarantee?


a) Order
b) No duplicates
c) Both
d) None of the above

Which List implementation is synchronized?


a) ArrayList
b) Vector
c) LinkedList
d) None of the above
Which interface represents a key-value pair mechanism?
a) List
b) Set
c) Queue
d) Map

Which method is used to check if a Collection is empty?


a) isEmpty()
b) isNull()
c) checkEmpty()
d) hasElements()

What does the Collections class sort() method do?


a) Sorts elements in descending order
b) Sorts elements in ascending order
c) Randomizes the order of elements
d) Removes duplicates

What is the key difference between HashSet and TreeSet?


a) HashSet allows duplicates; TreeSet doesn't
b) HashSet is ordered; TreeSet is unordered
c) HashSet is unordered; TreeSet is ordered
d) HashSet is synchronized; TreeSet is not

Which method is used to insert an object at a specific position in a List?


a) put()
b) set()
c) insert()
d) add()
Which interface provides methods to traverse through a collection?
a) Iterator
b) Enumerator
c) Traverser
d) Mover

You might also like