This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What is the output of the following code?
class Test {
static {
System.out.print("A");
}
public static void main(String[] args) {
System.out.print("B");
}
}
Option B
In Java, a static initialization block is executed automatically when the class is initialized, before the main() method starts executing. Therefore, the statement inside the static block prints A first.
After the static block finishes, the main() method executes and prints B. Hence, the final output is AB.
Q: 2Which of the following is the most important feature that makes Java suitable for Internet applications?
Option C
Java is widely used for Internet and web-based applications because of several important features. One of the key requirements for Internet applications is that programs must run on different platforms without modification and support networked or distributed computing.
Java fulfills these requirements through its Portability, meaning compiled Java bytecode can run on any system with a Java Virtual Machine (JVM), and its ability to work in a Distributed environment, allowing objects and programs to communicate over networks.
Q: 3What is the main role of Java Development Kit (JDK)?
Option D
The Java Development Kit (JDK) is a software development kit provided by Java to help programmers develop Java applications. It contains tools for writing, compiling, and running Java programs.
The JDK includes the Java Compiler (javac) to convert source code into bytecode, the Java Runtime Environment (JRE) to execute Java programs, and other development tools such as debuggers and documentation generators.
Q: 4Which of the following is a commonly used Object-Oriented Programming Language?
Option C
Java is a widely used Object-Oriented Programming (OOP) language that supports core OOP principles such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation.
Q: 5Which of the following statement is true about public and private members in Java?
Option D
In Java, access modifiers determine the visibility of class members. The two commonly used modifiers are public and private.
Private members are accessible only within the same class in which they are declared, they cannot be accessed from subclasses or other classes.
Public members are accessible from any other class, regardless of the package or subclass relationship.
MEMBER ACCESS RULE TABLE:
| ACCESS MODIFIER → ACCESS LOCATION ↓ | PUBLIC | PROTECTED | DEFAULT | PRIVATE |
|---|---|---|---|---|
| Same class. | Yes | Yes | Yes | Yes |
| Subclass in same package. | Yes | Yes | Yes | No |
| Other classes in same package. | Yes | Yes | Yes | No |
| Subclass in others package. | Yes | Yes | No | No |
| Non-subclasses in others package. | Yes | No | No | No |
Q: 6Which one of the following is true for Virtual Machine like JVM?
Option A
While Java as a language is platform-independent, the Java Virtual Machine (JVM) itself is hardware and platform dependent.
Every operating system (Windows, Linux, MacOS) and hardware architecture (x86, ARM) requires its own specific implementation of the JVM to bridge the gap between Java's bytecode and the host's native environment.
Q: 7Consider the following Java program
class Test
{
public static void main(String args[])
{
public int x=3;
protected int y=6;
private int z=9;
System.out.println(x+y+z);
}
}
What will be result of attempting to compile and run the program?
Option C
In Java, access modifiers like public, protected, and private cannot be used for local variables inside a method. These modifiers are only allowed for class-level variables (instance or static variables), not inside the main() method.
Q: 8What output is produced for the command: “java cmd a b c d”, when following code is run?
class cmd {
public static void main(String args[]) {
System.out.println(args[2]);
}
}
Option A
Command Line Argument are parameters that are supplied to the application program at the time of invoking it for execution. We can customize the behavior of the main() method using command-line arguments.
When command line arguments are supplied to JVM, JVM wraps these and supply to args[ ]. It can be confirmed that they are actually wrapped up in args array by checking the length of args using args.length.
For the command java cmd a b c d the arguments are assigned as follows:
The statement System.out.println(args[2]); will print the value stored at index 2, which is "c".
Q: 9Which command is used to convert the code written in Java to bytecode?
Option D
Compilation : Converting the Java source code (.java file) into bytecode (.class file) that can run on the Java Virtual Machine (JVM). The javac command is used to compile Java source code into bytecode.
javac FirstExample.java
Execution : Running the bytecode on the JVM. The java command is used to run the bytecode on the JVM.
java FirstExample
| COMMAND | USED FOR |
|---|---|
| javac | Compiles Java source code (.java) into bytecode (.class). |
| java | Runs the compiled Java bytecode on the Java Virtual Machine (JVM). |
| javadoc | Generates HTML documentation from Java source code comments. |
| javap | Disassembles bytecode (.class file) to view class structure, methods, and fields. |
| jar | Creates, views, or extracts Java Archive (.jar) files. |
| jdb | Java debugger. Used to debug Java programs. |
| jconsole | Java monitoring and management console for running Java applications. |
| jshell | Java REPL (Read-Eval-Print Loop) for running snippets of Java code interactively. |
Q: 10Which component is primarily responsible for converting Java bytecode into native instructions during execution?
Option D
When a Java program is compiled, the javac compiler converts Java source code (.java) into bytecode, which is stored in a .class file. This bytecode is platform-independent and cannot normally be executed directly by the CPU.
During execution, the JVM (Java Virtual Machine) is responsible for executing the bytecode. The JVM uses its execution mechanisms, particularly the JIT (Just-In-Time) compiler, to convert frequently executed bytecode into native machine instructions that can be directly executed by the underlying processor.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.