Java MCQ 100 Question Answer. These 100 Multiple Choice Questions on Java will help you score full marks in your exam and interview.
Table of Contents
Introduction to Java: Java MCQ 100 Question Answer
What is Java?
Question 1: Java was initially developed for:
A. Desktop applications
B. Interactive television
C. Mobile devices
D. Web servers
Question 2: Which of the following statements is true about Java?
A. Java is a procedural programming language.
B. Java is a platform-dependent language.
C. Java is an interpreted language.
D. Java is an object-oriented programming language.
Question 3: Who developed Java?
A. James Gosling
B. Bjarne Stroustrup
C. Guido van Rossum
D. Dennis Ritchie
Features of Java
Question 4: What makes Java “write once, run anywhere”?
A. Platform independence
B. Object-oriented nature
C. Automatic garbage collection
D. Multithreading
Question 5: Which feature of Java allows code reusability?
A. Platform independence
B. Object-oriented nature
C. Automatic garbage collection
D. Multithreading
YQuestion 6: Java’s ability to handle multiple tasks concurrently is known as:
A. Platform independence
B. Object-oriented nature
C. Automatic garbage collection
D. Multithreading
Different editions of Java
Question 7: Which Java edition is used for developing enterprise-level applications?
A. Java SE
B. Java EE
C. Java ME
D. Java FX
Question 8: Which Java edition is used for developing applications for resource-constrained devices?
A. Java SE
B. Java EE
C. Java ME
D. Java FX
Understanding the Java Virtual Machine (JVM) and bytecode
Question 9: What is the role of the Java Virtual Machine (JVM)?
A. Compiling Java code into machine code
B. Executing Java bytecode
C. Writing Java code
D. Debugging Java code
Question 10: What is the output of the Java compiler?
A. Source code
B. Bytecode
C. Machine code
D. Executable file
Setting up the Java Development Environment
Question 11: Which of the following is the core component of the Java Development Kit (JDK)?
A. Java Runtime Environment (JRE)
B. Java Virtual Machine (JVM)
C. Java compiler
D. All of the above
Question 12: What is the command used to compile a Java program from the command line?
A. java
B. javac
C. javadoc
D. jar
Question 13: What does JDK stand for?
A. Java Development Kit
B. Java Deployment Kit
C. Java Design Kit
D. Java Debugging Kit
Setting up environment variables
Question 14: Which environment variable is used to specify the location of the Java compiler?
A. PATH
B. CLASSPATH
C. JAVA_HOME
D. JRE_HOME
Question 15: What is the purpose of the CLASSPATH environment variable?
A. To specify the location of the Java compiler
B. To specify the location of Java class files
C. To specify the location of the Java Runtime Environment (JRE)
D. To specify the location of the Java Virtual Machine (JVM)
Question 16: Which of the following is NOT a common way to set environment variables?
A. Through the command line
B. Through system properties
C. Through an IDE’s settings
D. Through a web browser
Introduction to Integrated Development Environments (IDEs)
Question 17: Which of the following is a popular IDE for Java development?
A. Eclipse
B. IntelliJ IDEA
C. NetBeans
D. All of the above
Question 18: What is the primary advantage of using an IDE for Java development?
A. It provides a text editor to write code.
B. It helps in compiling and running Java code.
C. It offers tools for debugging and testing.
D. All of the above
Question 19: Which IDE is known for its intelligent code completion and refactoring capabilities?
A. Eclipse
B. IntelliJ IDEA
C. NetBeans
D. Visual Studio Code
Question 20: Which of the following is NOT a feature typically found in Java IDEs?
A. Code editor with syntax highlighting
B. Built-in debugger
C. Version control integration
D. Web server deployment
Java Fundamentals: Java MCQ 100 Question Answer
Basic Syntax and Data Types
Question 21: What is the entry point for execution in a Java program?
A. main method
B. class definition
C. import statement
D. package declaration
Question 22: What is the correct syntax for declaring a class in Java?
A. `classname { }`
B. `class classname { }`
C. `public class classname { }`
D. `Class classname { }`
YQuestion 23: Which keyword is used to define a method in Java?
A. method
B. function
C. void
D. define
Question 24: Which of the following is NOT a primitive data type in Java?
A. int
B. String
C. boolean
D. double
Question 25: What is the default value of an int variable in Java?
A. 0
B. 1
C. null
D. undefined
Question 26: Which data type is used to store single characters in Java?
A. char
B. string
C. character
D. text
Question 27: Which data type is used to store decimal numbers in Java?
A. int
B. float
C. double
D. long
Question 28: What is the size of a boolean variable in Java?
A. 1 byte
B. 2 bytes
C. 4 bytes
D. JVM dependent
Operators
Question 29: Which operator is used to add two numbers in Java?
A. +
B. –
C. *
D. /
Question 30: Which operator is used to compare two values for equality in Java?
A. =
B. ==
C. !=
D. >
Question 31: Which operator is used to perform logical AND in Java?
A. &
B. &&
C. |
D. ||
Question 32: Which operator is used to perform a bitwise complement in Java?
A. ~
B. !
C. ^
D. <<
Question 33: What is the result of 10 % 3 in Java?
A. 0
B. 1
C. 3
D. 10
Type Casting and Conversions
Question 34: What is the process of converting a larger data type to a smaller data type called?
A. Implicit casting
B. Explicit casting
C. Type promotion
D. Type demotion
Question 35: What happens when you try to assign a double value to an int variable without casting?
A. The code compiles without error.
B. A runtime error occurs.
C. A compile-time error occurs.
D. The double value is truncated to fit into the int variable.
Control Flow Statements
Question 36: Which statement is used to execute a block of code based on a condition?
A. `for` loop
B. `while` loop
C. `if-else` statement
D. `switch` statement
Question 37: Which statement allows you to choose from multiple code blocks based on the value of an expression?
A. `for` loop
B. `while` loop
C. `if-else` statement
D. `switch` statement
Question 38: What is the keyword used to indicate the default case in a `switch` statement?
A. `else`
B. `default`
C. `case`
D. `otherwise`
Question 39: Which loop is used when the number of iterations is known beforehand?
A. `for` loop
B. `while` loop
C. `do-while` loop
D. `foreach` loop
Question 40: Which loop executes the code block at least once, even if the condition is initially false?
A. `for` loop
B. `while` loop
C. `do-while` loop
D. `foreach` loop
Question 41: Which statement is used to exit a loop prematurely?
A. `break`
B. `continue`
C. `exit`
D. `return`
Question 42: Which statement is used to skip the current iteration of a loop and proceed to the next?
A. `break`
B. `continue`
C. `skip`
D. `next`
Object-Oriented Programming (OOP) Concepts
Classes and Objects
Question 43: What is a blueprint or template that defines the characteristics and behavior of objects?
A. Class
B. Object
C. Method
D. Variable
Question 44: What is an instance of a class?
A. Object
B. Method
C. Variable
D. Constructor
Question 45: Which keyword is used to create an object in Java?
A. `class`
B. `new`
C. `object`
D. `instance`
Question 46: What are variables declared within a class called?
A. Instance variables
B. Local variables
C. Class variables
D. Global variables
Question 47: What are functions defined within a class called?
A. Methods
B. Procedures
C. Functions
D. Subroutines
Question 48: Which of the following is NOT a characteristic of instance variables?
A. They are declared inside a class.
B. They are associated with each object of the class.
C. They are accessible from any other class.
D. They have default values if not explicitly initialized.
Access Modifiers
Question 49: Which access modifier makes a member accessible only within the same class?
A. `public`
B. `private`
C. `protected`
D. `default`
Question 50: Which access modifier makes a member accessible within the same package and to subclasses?
A. `public`
B. `private`
C. `protected`
D. `default`
YQuestion 51: Which access modifier makes a member accessible from any other class?
A. `public`
B. `private`
C. `protected`
D. `default`
YConstructors
Question 52: What is a special method used to initialize objects?
A. Constructor
B. Method
C. Class
D. Initializer
Question 53: What happens if a class does not have an explicitly defined constructor?
A. A compile-time error occurs.
B. A default constructor is provided by the compiler.
C. The class cannot be instantiated.
D. The superclass constructor is called.
Inheritance
Question 54: What is the mechanism by which a class acquires the properties and behaviors of another class?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Abstraction
Question 55: What is the class being inherited from called?
A. Subclass
B. Superclass
C. Parent class
D. Both B and C
Question 56: Which type of inheritance involves a single parent class and a single child class?
A. Single inheritance
B. Multilevel inheritance
C. Hierarchical inheritance
D. Multiple inheritance
Question 57: Which type of inheritance involves a chain of inheritance where a class inherits from another class, which in turn inherits from another class?
A. Single inheritance
B. Multilevel inheritance
C. Hierarchical inheritance
D. Multiple inheritance
Question 58: Which type of inheritance involves a single parent class having multiple child classes?
A. Single inheritance
B. Multilevel inheritance
C. Hierarchical inheritance
D. Multiple inheritance
Overriding Methods
Question 59: What is the process of redefining a method in a subclass that is already defined in its superclass?
A. Method overloading
B. Method overriding
C. Method hiding
D. Method delegation
Question 60: Which annotation is used to indicate that a method overrides a method in its superclass?
A. `@Override`
B. `@Overload`
C. `@Inherited`
D. `@OverrideMethod`
YThe super keyword
Question 61: Which keyword is used to refer to the immediate parent class in Java?
A. `this`
B. `super`
C. `parent`
D. `base`
Question 62: Which of the following can be accessed using the `super` keyword?
A. Instance variables of the superclass
B. Methods of the superclass
C. Constructors of the superclass
D. All of the above
Object-Oriented Programming (OOP) Concepts (Cont.)
Polymorphism
Question 63: What is the ability of an object to take on many forms?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Abstraction
Question 64: Which of the following is an example of compile-time polymorphism?
A. Method overriding
B. Method overloading
C. Dynamic binding
D. Interface implementation
Question 65: Which of the following is an example of runtime polymorphism?
A. Method overriding
B. Method overloading
C. Constructor overloading
D. Static method invocation
Question 66: What is the mechanism by which the appropriate method to be called is determined at runtime?
A. Static binding
B. Dynamic binding
C. Method resolution
D. Compile-time binding
Question 67: Which of the following is NOT related to dynamic binding?
A. Method overriding
B. Inheritance
C. Method overloading
D. Abstract classes
Abstract Classes and Interfaces
Question 68: What is a class that cannot be instantiated and is meant to be subclassed?
A. Concrete class
B. Abstract class
C. Interface
D. Final class
Question 69: What is a blueprint that defines a set of methods that a class must implement?
A. Concrete class
B. Abstract class
C. Interface
D. Final class
Question 70: Which keyword is used to declare an interface in Java?
A. `class`
B. `abstract`
C. `interface`
D. `implements`
Working with Data: Java MCQ 100 Question Answer
Arrays
Question 71: What is a container that stores a fixed number of elements of the same data type?
A. Array
B. List
C. Set
D. Map
Question 72: Which of the following is the correct way to declare an array in Java?
A. `int array[5];`
B. `int[] array = new int[5];`
C. `int array = new int[5];`
D. `array{5};`
Question 73: How do you access the first element of an array named “array” in Java?
A. `array[1]`
B. `array[0]`
C. `array.first()`
D. `array.get(0)`
Question 74: What is the length of an array named “array” in Java?
A. `array.length()`
B. `array.length`
C. `array.size()`
D. `array.size`
YQuestion 75: What is an array that contains other arrays as its elements?
A. Single-dimensional array
B. Multidimensional array
C. Jagged array
D. Both B and C
Question 76: How do you declare a two-dimensional array in Java?
A. `int[][] matrix = new int[3][4];`
B. `int matrix[][] = new int[3][4];`
C. `int[3][4] matrix;`
D. Both A and B
YStrings
Question 77: Which of the following is NOT a valid String method in Java?
A. `length()`
B. `concat()`
C. `substring()`
D. `append()`
Question 78: What does the `toUpperCase()` method do?
A. Converts a string to lowercase
B. Converts a string to uppercase
C. Returns the length of a string
D. Compares two strings
Question 79: What does it mean that Strings are immutable in Java?
A. Strings cannot be changed after they are created.
B. Strings can be modified after they are created.
C. Strings can be null.
D. Strings can be empty.
Question 80: Which of the following classes is mutable and can be used to efficiently manipulate strings?
A. `String`
B. `StringBuilder`
C. `StringBuffer`
D. Both B and C
Question 81: What is the main difference between `StringBuilder` and `StringBuffer`?
A. `StringBuilder` is thread-safe, while `StringBuffer` is not.
B. `StringBuffer` is thread-safe, while `StringBuilder` is not.
C. `StringBuilder` is immutable, while `StringBuffer` is mutable.
D. `StringBuffer` is immutable, while `StringBuilder` is mutable.
Question 82: Which class is generally preferred for single-threaded string manipulation due to its performance advantage?
A. `String`
B. `StringBuilder`
C. `StringBuffer`
D. None of the above
Advanced Concepts
Exception Handling
Question 83: What is an abnormal event that disrupts the normal flow of a program?
A. Error
B. Exception
C. Bug
D. Fault
Question 84: Which of the following is NOT a common type of exception in Java?
A. `IOException`
B. `ArithmeticException`
C. `NullPointerException`
D. `ArrayIndexOutOfBoundsException`
Question 85: What is the purpose of exception handling?
A. To prevent the program from crashing
B. To gracefully handle errors
C. To provide informative error messages
D. All of the above
Question 86: Which block is used to enclose the code that might throw an exception?
A. `try`
B. `catch`
C. `finally`
D. `throw`
Question 87: Which block is used to handle the exception?
A. `try`
B. `catch`
C. `finally`
D. `throw`
Question 88: Which block is always executed, regardless of whether an exception is thrown or not?
A. `try`
B. `catch`
C. `finally`
D. `throw`
Question 89: What is the purpose of the `finally` block?
A. To handle the exception
B. To execute code regardless of whether an exception occurs
C. To throw an exception
D. To log the exception
Question 90: Which keyword is used to explicitly throw an exception?
A. `try`
B. `catch`
C. `finally`
D. `throw`
Question 91: What is a custom exception?
A. An exception defined by the Java platform
B. An exception defined by the programmer
C. An exception that cannot be caught
D. An exception that cannot be thrown
Collections Framework
Question 92: What is a framework that provides interfaces and classes for storing and manipulating groups of objects?
A. Arrays
B. Collections Framework
C. Data Structures
D. Generics
Question 93: Which of the following is NOT a benefit of using the Collections Framework?
A. Reduced programming effort
B. Increased performance
C. Improved code readability
D. Enhanced security
Question 94: Which interface represents an ordered collection of elements?
A. `List`
B. `Set`
C. `Map`
D. `Queue`
Question 95: Which interface represents an unordered collection of unique elements?
A. `List`
B. `Set`
C. `Map`
D. `Queue`
Question 96: Which class provides a resizable array implementation of the `List` interface?
A. `ArrayList`
B. `LinkedList`
C. `Vector`
D. `Stack`
Question 97: Which class implements the `Map` interface using a hash table?
A. `HashMap`
B. `TreeMap`
C. `LinkedHashMap`
D. `Hashtable`
File Handling
Question 98: Which class is used to read data from a file?
A. `FileReader`
B. `FileWriter`
C. `FileInputStream`
D. `FileOutputStream`
Question 99: Which class is used to write data to a file?
A. `FileReader`
B. `FileWriter`
C. `FileInputStream`
D. `FileOutputStream`
Question 100: Which of the following is NOT a common file format in Java?
A. Text files (.txt)
B. CSV files (.csv)
C. XML files (.xml)
D. MP3 files (.mp3)
Most Asked Important Questions on JAVA
What are the key features of JAVA?
Object-oriented: JAVA is designed around the concept of objects, making code organization and reusability easier.
Platform-independent: JAVA applications can run on any operating system with a Java Virtual Machine (JVM).
Secure: JAVA has built-in security features that protect against malicious code
What are some common uses of JAVA?
Android app development
Web applications
Enterprise software
Big data technologies
What are the basic concepts in JAVA?
Variables: Used to store data.
Data types: Define the type of data a variable can hold.
Operators: Perform operations on data.
Control flow statements: Determine the order in which code is executed.
Classes and objects: Fundamental building blocks of object-oriented programming.
Read Also: Artificial Intelligence Best 100 MCQs