Java Question Bank with Answers
Answers for Core Java Question Bank.
Chapter 1: Introduction to Core Java.
Easy Level Questions (5 Marks Each)
1. What is Java? Briefly describe its origin and list two key reasons for its widespread adoption.
Answer:
Java is a high-level, object-oriented, class-based, concurrent, secure, and
platform-independent programming language specifically designed for the
Internet.
- Origin: Java was originally developed by James Gosling at Sun Microsystems (now owned by Oracle Corporation) and released in 1995. It was initially named "Oak."
- Reasons for Widespread Adoption (any two):
1. Platform Independence ("Write Once, Run Anywhere"): Java code is compiled into bytecode, which can run on any device with a Java Virtual Machine, making it highly portable.
2. Object-Oriented Programming Paradigm: Java inherently supports core OOP principles (encapsulation, inheritance, polymorphism, abstraction), which promotes modularity, reusability, and easier maintenance of large-scale applications. Other reasons include its robust memory management (garbage collection), strong security features, and suitability for network-centric applications.
2. List any three prominent features or "buzzwords" of Java (e.g., platform independence, object-oriented, secure). Briefly explain one of them.
Answer:
Three prominent features of Java are:
1. Platform Independent
2. Object-Oriented
3. Secure
4. (Other options: Simple, Robust, Multithreaded, High Performance, Distributed)
Explanation of one feature
(e.g., Platform Independent):
Platform Independent: Java achieves platform independence through its
compilation process. Source code (.java
files) is compiled into an intermediate format called bytecode (.class
files) by the Java compiler. This bytecode is then executed by the Java Virtual
Machine. Since a JVM is available for almost every operating system and
hardware platform, the same bytecode can run on different platforms without
recompilation, thus realizing the "Write Once, Run Anywhere"
principle.
3. Name any two commonly used Integrated Development Environments for Java programming. Discuss one benefit of using an IDE over a text editor for writing Java code.
Answer:
Two commonly used Integrated Development Environments for Java programming are:
1. Eclipse
2. NetBeans
3. (Other options: IntelliJ IDEA, VS Code with Java extensions)
Benefit of using an IDE over a
text editor:
One significant benefit of using an IDE over a simple text editor is enhanced
productivity and code quality. IDEs provide features like:
- Code Autocompletion: Suggests method names, variables, and classes as you type, reducing typing errors and speeding up coding.
- Syntax Highlighting: Makes code more readable by coloring different elements (keywords, strings, comments).
- Real-time Error Checking: Identifies syntax errors and potential issues as you type, allowing for immediate correction.
- Integrated Debugging Tools: Simplifies finding and fixing bugs by allowing developers to set breakpoints, step through code, and inspect variable states.
- Refactoring Tools: Helps in restructuring code without changing its external behavior, improving maintainability.
4. Differentiate between single-line (//) and multi-line (/* … */) comments in Java with an example of each. When would you use one over the other?
Answer:
Comments in Java are used to add explanations or notes within the code, making
it more understandable for developers. They are ignored by the compiler.
- Single-line Comments (//):
- Description: These comments start with // and extend only to the end of the current line.
- Example:
int age = 25; // Declares an integer variable for age
- Usage: Best suited for short explanations, annotations, or temporarily disabling a single line of code.
- Multi-line Comments (/* … */):
- Description: These comments start with /* and end with */. Any text between these delimiters is considered a comment, spanning multiple lines.
- Example:
o /*
o * This is a multi-line comment.
o * It can span across several lines
o * to provide detailed explanations.
o */
double price = 99.99;
- Usage: Ideal for providing longer descriptions, commenting out blocks of code, or for license information at the top of a file. Javadoc comments (/** … */) are a special type of multi-line comment used for generating API documentation.
5. List and briefly describe any five of the eight primitive data types available in Java, mentioning their typical use cases.
Answer:
Java has eight primitive data types, categorized into four groups: integer,
floating-point, character, and boolean. Here are five of them:
1. int:
o Description: A 32-bit signed two’s complement integer.
o Use Case: Most commonly used for general-purpose integer values, such as loop counters, array indices, or counts.
o Example: int studentCount = 150;
2. double:
o Description: A 64-bit double-precision floating-point number.
o Use Case: Used for representing decimal numbers with high precision, suitable for scientific calculations, currency, or measurements.
o Example: double temperature = 98.6;
3. boolean:
o Description: Represents one of two possible values: true or false.
o Use Case: Used for conditional logic, flags, or to store truth values in logical operations.
o Example: boolean isActive = true;
4. char:
o Description: A 16-bit Unicode character.
o Use Case: Used to store single characters, like letters, digits, or symbols.
o Example: char grade = ‘A’;
5. byte:
o Description: An 8-bit signed two’s complement integer.
o Use Case: Useful for saving memory in large arrays, especially when working with data streams or raw binary data. Smallest integer type.
o Example: byte dataValue = 100;
(Other primitive types: short, long, float)
6. Define a "final variable" in Java. Explain with an example why a final variable’s value cannot be changed after its initial assignment.
Answer:
In Java, a final variable is a
variable whose value, once initialized, cannot be changed (reassigned)
throughout its lifetime. It essentially makes the variable a constant.
Explanation and Example:
When the final keyword is applied to a variable, it signifies
that the variable’s value is fixed. The Java compiler enforces this rule. If an
attempt is made to modify a final variable after it has been
given an initial value, the compiler will generate a compilation error. This
mechanism ensures data integrity for values that are not meant to be altered,
such as mathematical constants or configuration settings.
Example:
public class FinalVariableExample {
public static void main(String[] args) {
// Declare and initialize a final variable
final double PI = 3.14159;
System.out.println("Initial PI value: " + PI);
// Attempt to change the value of PI
// PI = 3.0; // This line would cause a compile-time error:
// "cannot assign a value to final variable PI"
// Declare a final variable in a method parameter or locally
final String GREETING = "Hello, Java!";
System.out.println("Greeting: " + GREETING);
// GREETING = "Hi!"; // This would also be a compile-time error
}
}
In this example, PI is declared as final. Any subsequent attempt to assign a new value to PI will result in a compile-time error, clearly demonstrating that its value is immutable once set.