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: 1Which of the following is true about JSP?
Option B
In JSP (Java Server Pages) technology, JSP pages are first translated into Java servlets. These servlets are then compiled and executed by the web server. This allows JSP to create dynamic and interactive web pages.
<%@ page language="java" %>
<html>
<body>
<h1>Hello, JSP Example</h1>
<%
String name = "Suresh";
out.println("Welcome, " + name);
%>
</body>
</html>
When you save this file as myfile.jsp and run it on a server like Tomcat, the server automatically converts it into a servlet like this:
public final class myfile_jsp extends HttpJspBase {
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
JspWriter out = response.getWriter();
out.write("<html><body>");
out.write("<h1>Hello, JSP!</h1>");
String name = "Suresh";
out.write("Welcome, " + name);
out.write("</body></html>");
}
}
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: 3Read the following code snippet and answer
class Rectangle
{
private int length;
private int breadth;
public int area=0;
public Rectangle()
{
this.length=0;
this.breadth=0;
this.area=0;
}
public Rectangle(int l, int b)
{
this.length=l;
this.breadth=b;
this.area=l*b;
}
public static void main(String a[])
{
Rectangle A = new Rectangle(20,5);
System.out.println(A.area);
}
}
Option B
In this program, the class Rectangle contains:
There are two constructors:
Default Constructor : Sets all values to 0.
Parameterized constructor : Assigns length=l, breadth=b, and calculates area=l*b.
When the statement Rectangle A=new Rectangle(20,5); is executed, the parameterized constructor is called because two arguments are passed. Inside this constructor, length is assigned the value 20, breadth is assigned 5, and area is calculated as l*b, which becomes 20*5 = 100.
After object creation, System.out.println(A.area); prints the value stored in the instance variable area, i.e., 100.
There is no compilation or runtime error because the variable area is properly declared as public int area and initialized.
Q: 4What will be the output of the following program?
public class Demo {
public static void main(String[] args) {
int[] arr = {120, 200, 016};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); }
}
}
Option A
In Java, numbers can be represented in different formats, such as decimal, octal, hexadecimal, and binary. By default, numbers written without any prefix are decimal numbers (base 10).
However, if a number starts with a 0, Java treats it as an octal number (base 8). Octal numbers use digits from 0 to 7 only.
If a number starts with 0x, Java treats it as an hexadecimal number (base 16). Hexadecimal numbers use digits from 0 to 9 and a to f.
In the given program, the array is declared as int[] arr = {120, 200, 016};. Here, 120 and 200 are standard decimal numbers. The third element, 016, starts with a 0, so Java interprets it as an octal number. To understand its decimal value, we convert octal 16 to decimal, 1×81+6×80 = 8+6 = 14. Therefore, the array actually contains the values {120, 200, 14}.
The for loop then iterates through this array and prints each element followed by a space. As a result, the output of the program is 120 200 14.
Q: 5What do you mean by Method Overloading in Java?
Option C
In Java, a method is a block of code that performs a specific task. One of the core features of Java is Polymorphism, which allows objects or methods to take multiple forms.
Method overloading is a type of compile-time polymorphism where a class has two or more methods with the same name but different parameter lists.
It allows programmers to use the same method name for related actions, improving readability and organization.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(15, 25));
System.out.println(calc.add(15, 12, 30));
System.out.println(calc.add(2.5, 7.5));
}
}
Q: 6What does the following code do?
public class Example {
private int var;
public Example(int var) { this.var = var; }
}
Option C
In this code, the class Example has a private instance variable named var. The constructor Example(int var) receives a parameter with the same name. Inside the constructor, the statement this.var = var; is used to distinguish between the instance variable and the constructor parameter.
Here, this.var refers to the instance variable of the current object, while var refers to the parameter. Therefore, the code simply assigns the value passed into the constructor to the object's own variable.
Q: 7What 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: 8Which 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: 9Which of the following correctly declares a method that accepts one dimensional array of double as a parameter?
Option B
When a method needs to accept an array as a parameter, the arrays type must be specified correctly. For a one-dimensional array of double, the syntax is double[] arr, where double[] indicates an array of double values and arr is the name of the parameter.
Q: 10Which of the following is false about Vector class in Java?
Option D
In Java, a Vector is a class that implements a dynamic array, which means it can grow or shrink automatically as elements are added or removed.
Q: 11What will be the value of ‘val’ displayed when JSP page is accessed for the third time since server startup?
<%! int val = 0; %>
<% val++; %>
<html><body><p>val : <% = val %></p></body></html>
Option C
In JSP (Java Server Pages), understanding how variables behave across multiple requests is essential. JSP provides different scripting elements such as declarations <%! %>, scriptlets <% %>, and expressions <%= %>, each with a specific role and scope.
Declarations <%! %>
Scriptlets <% %>
| Request Number | Initial val | Operation (val++) | Value Displayed |
|---|---|---|---|
| 1st Request | 0 | 0 to 1 | 1 |
| 2nd Request | 1 | 1 to 2 | 2 |
| 3rd Request | 2 | 2 to 3 | 3 |
Q: 12Which 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: 13What will be the output of the following code?
public class Demo {
public static void main(String[] args) {
String str = “Hello”;
str = str+”World”;
str = str.substring(5,10);
System.out.println(str);
}
}
Option D
The initial string str is assigned the value "Hello". Then, str is concatenated with "World" resulting in the string "HelloWorld". Next, the substring(5, 10) method extracts characters starting at index 5 to index 9 (10 is excluded).
The string str = "HelloWorld" has indices:
| Index | str[0] | str[1] | str[2] | str[3] | str[4] | str[5] | str[6] | str[7] | str[8] | str[9] |
|---|---|---|---|---|---|---|---|---|---|---|
| Value | H | e | l | l | o | W | o | r | l | d |
So, in "HelloWorld", index 5 corresponds to the character 'W' and index 9 corresponds to 'd'. Thus, the substring "World" is extracted and printed.
Q: 14In the statement ‘System.out.println("Hello World!")’, the keyword 'out' stands for the following—
Option A
In Java, System.out.println("Hello World!") is used to print messages to the console.
| COMPONENT | TYPE | DESCRIPTION |
|---|---|---|
| System | System is a class, defined in java.lang package. | Provides access to standard input, output, and error streams; part of Java core library. |
| out | public static member of System class. | The out is an instance of the PrintStream class, which provides methods like println() to display output. |
| println() | Method of PrintStream class. | Prints the given message or value to the console and moves to the next line. |
Q: 15Which 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.
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.