Pra MCQ
Pra MCQ
-> getMessage()
RuntimeException
CompilationError
ExceptionHandled
None of the above
class MethodOverloading{
void display(int a,float b) { // Method 1
System.out.println("Method 1");
System.out.println(a+b);
}
void display(int a,int b) { // Method 2
System.out.println("Method 2");
System.out.println(a+b);
}
void display(int a,long b) { // Method 3
System.out.println("Method 3");
System.out.println(a+b);
}
void display(int a,double b) {
System.out.println("Method 4");
System.out.println(a+b); // Method 4
}
}
public class DemoClass{
public static void main(String[] args) {
MethodOverloading mo=new MethodOverloading();
mo.display(10, 10.5); // Statement 1
}
}
Method 1
Method 2
Method 3
Method 4
-> Method 4
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
System.out.println("Dog Barks");
}
}
Lion roars
Dog Barks
Dog Barks
Lion roars
Lion roars
Dog Barks
Animal makes a sound
->
Lion roars
Dog Barks
class DemoClass {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(1000);
stk.pop();
stk.push(2000);
stk.push(3000);
stk.pop();
stk.push(4000);
System.out.println(stk.size());
}
}
2
3
5
4
-> 2
Sameer is trying to execute following code . what will be the output after
execution ?
public class DemoClass {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("Chennai");
list.add("Mumbai");
list.add("Kolkata");
list.add("Bangalore");
int a = (int) list.get(0);
System.out.println(list);
}
}
ArrayIndexOutOfBoundException
ClassCastException
InputMismatchException
[Chennai, Mumbai, Kolkata, Bangalore]
->ClassCastException
NullPointerException
FileNotFoundException
ClassNotFoundException
IOException
->NullPointerException
Which method is used to retrieve the auto-generated keys after executing an INSERT
statement in JDBC ?
executeUpdate()
executeQuery()
getGeneratedKeys()
getResultSet()
->getGeneratedKeys()
Which JDBC interface is used to execute a parameterized SQL query to retrieve data
from a database?
Statement
ResultSet
PreparedStatement
CallableStatement
->PreparedStatement
->Driver.getConnection(url)
Jay is trying to connect with database. help jay by selecting correct method to
connect with Database ?
connect( )
openConnection( )
getConnection( )
createConnection( )
->getConnection()
Rahul is trying to end the database connection. help rahul to end the database
connection by using proper method?
shutdown()
close()
endconnection()
disconnect(
-> close()
->
1. Register the driver class
2. Creating connection
3. Creating statements
4. Executing queries
5. Closing connection
->SQLException
Hritik is trying to establish the connection with the Apche derby database.
Help Hritik to choose correct code snippet
Class.forName("org.mysql.jdbc.EmbeddedDriver");
con=DriverManager.getConnection("jdbc:derby:C:\\User\\MyDB;create=true");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con=DriverManager.getConnection("jdbc:derby:C:\\User\\MyDB;create=true");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con=DriverManager.getConnection("derby:C:\\User\\MyDB;create=true");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con=DriverManager.getConnection("jdbc:C:\\User\\MyDB;create=true");
->
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con=DriverManager.getConnection("jdbc:derby:C:\\User\\MyDB;create=true");
Which of the following code is used to get a HTTP Session object in servlets?
request.getSession()
response.getSession();
new Session()
None of the above
->request.getSession()
Which of the following code is used to get an attribute in a HTTP Session object in
servlets?
session.getAttribute(String name)
session.alterAttribute(String name)
session.updateAttribute(String name)
session.setAttribute(String name)
->session.getAttribute(String name)
URL rewriting
Hidden form fields
SSL sessions
Cookies
-> Cookies
<jsp:forward>
<%@directive%>
response.sendRedirect(URL)
response.setRedirect(URL)
-> response.sendRedirect(URL)
-> HttpServletRequest
The doGet() method in the example extracts values of the parameter’s type and
number by using __________
request.getParameter()
request.setParameter()
response.getParameter()
response.getAttribute()
-> request.getParameter()
->cannot be null
Delete
Purge
Remove
Drop table
->Drop table
-> LENGTH()
With SQL, how do you select all the records from a table named "Toddler" where the
value of the column "Name" starts with "A"?
SELECT * FROM Toddler WHERE Name LIKE '%A'
SELECT * FROM Toddler WHERE Name='A'
SELECT * FROM Toddler WHERE Name LIKE 'A%'
SELECT * FROM Toddler WHERE Name='%A%'
Write a sql query to update the designation as 'ITA' when location is null?
Inserting any value in the wrong index as shown below will result in
int A[]=new int[3];
A[5]=5;
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
Program executes successfully
-> ArrayIndexOutOfBoundsException
is
easy
error
easy to learn
->easy to learn
TRUE
FALSE
-> TRUE
class Demo {
public static void main(String[] args)
{
Short a=365;
System.out.println(a++);
}}
What is the output of the above code?
366
TRUE
365
error
->365
Java servlet
Java applet
Both A and B
None of the above
query
update
statement
None of the above
->query
javax.servlet.jsp.PrintWriter
javax.servlet.jsp.SessionWriter
javax.servlet.jsp.SessionPrinter
javax.servlet.jsp.JspWriter
-> javax.servlet.jsp.PrintWriter
-> doGet
-> id,class
Arithmetic Exception
Success
Arithmetic Exception
Arithmetic Exception
Success
->Arithmetic Exception
Which of the following is the correct order of blocks when using exception Handling
in java ?
->finally' Block
ResultSet.next()
Statement.executeQuery()
Connection.prepareStatement()
DriverManager.getConnection()
-> DriverManager.getConnection()
-> Throwable
java.lang.Object
└── java.lang.Throwable <-- (Superclass of all exceptions)
├── java.lang.Exception <-- (For checked and unchecked exceptions)
│ ├── java.io.IOException
│ ├── java.sql.SQLException
│ ├── java.lang.RuntimeException <-- (Superclass of unchecked exceptions)
│ ├── java.lang.ArithmeticException
│ ├── java.lang.NullPointerException
│ ├── java.lang.ArrayIndexOutOfBoundsException
└── java.lang.Error <-- (For serious system-level errors)
├── java.lang.OutOfMemoryError
├── java.lang.StackOverflowError
ArrayIndexOutOfBoundsException
ClassCastException
IllegalArgumentException
NumberFormatException
-> ClassCastException
->It indicates that the method may throw certain types of exceptions
Which among them is/are the following ways to declare 1-d/2-d arrays in Java?
(1)int[][] arr={{1,2,3},{11,12,13},{21,22,23}};
(2)int b[][]={{1,2,3},{11,12,13},{21,22,23}};
(3)int ar[]={1,2,3};
(4)int[] br={11,22,33};
(1),(2),(3),(4)
(1) and (4) only
(2) and (4) only
(2) and (3) only
->(1),(2),(3),(4)
Jane wants to create a list if Student name that allows duplicate entries and the
insertion order .
which java collection should she use ?
HashSet
LinkedHashSet
ArrayList
LinkedList
-> ArrayList
What is the type of inheritance demonstrated below?
public class Vehicles {
......
}
public class Cars extends Vehicles {
......
}
public class Trucks extends Vehicles {
......
}
public class Bus extends Vehicles {
......
}
Single
Multiple
Multilevel
Hierarchical
-> Hierarchical
-> class
Consider that we have declared a priority queue of type String-queue that contains
the following elements that are added to the queue in the following
order: Amrita,Vijay,Jaikar,Raj.Assuming the code is syntactically correct,What will
be ouput if the following statement?
System.out.println(queue.element());
Amrita
Raj
Amrita
Raj
Jaikar
Vijay
-> Amrita
Consider we have created an ArrayList of type String . The following elements are
added to the list in the following order-Ravi,Vijay,Chavi,Ajay,Mani.Assuming
the code is syntactically correct,What will be the output if the following code ?
import java.util.*;
............
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
..........
Ravi
Mani
Vijay
Chavi
Ajay
Ravi
Vijay
Mani
Chavi
Ajay
Ravi
Vijay
Chavi
Mani
Ajay
Ravi
Vijay
Chavi
Ajay
Mani
->
Vijay
Chavi
Ajay
Mani
Consider we have created a List of integers which consists of number 1,2,3 which
are added to the list in the same order.Assuming the code is written
syntactically correct,What is the output of the following code?
int removedNumber = numbers.remove(1);
System.out.println("Removed Element: " + removedNumber);
Removed Element: 1
Removed Element: 3
Removed Element: 2
Error is displayed
Car started
Bike started
Bike started
Car started
Compilation error
->
Car started
Bike started
What will be the expected output of the following snippet : Set<String> set = new
TreeSet<>();
set.add("banana");
set.add("apple");
set.add("mango");
System.out.println(set);
->
Array index out of bounds: Index 10 out of bounds for length 5
0
Finally block executed
[4,5]
[1,2,3,4,5]
[4,5,4,5]
[6,7,8]
->[4,5]
{null=Fest}
{null=Test}
compilation error at 7 and 8
compilation error at 8, about can't override the value "Test"
-> {null=Fest}
System.out.println("Eating");
}
}
class Bird extends Animal {
void eat() {
System.out.println("Bird eating");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Bird();
a.eat();
}
}
Eating
Bird eating
Compilation Error
Runtime Error
import java.util.*;
class Main {
public static void main(String[] args)
{
LinkedList<String> list = new LinkedList<>();
list.add("First");
list.add("Second");
list.addFirst("Third");
list.addFirst("Zero");
System.out.println(list.getFirst());
}
}
First
Second
Third
Zero
->Zero
import java.util.*;
class Main {
public static void main(String[] args)
{
try {
ArrayIndexOutOfBoundsException caught
Finally block executed
Compilation error
_>
ArrayIndexOutOfBoundsException caught
Finally block executed
import java.util.*;
class Main {
public static void main(String[] args)
{
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("A");
set.add("C");
set.add("A");
set.add("C");
set.add("B");
set.add("D");
System.out.println(set.size());
}
}
8
4
3
2
->4
import java.util.*;
class Parent {
void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
void display() {
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}
->
Parent
Compilation Error
Runtime Error
Child
->Child
import java.util.*;
[A, C, B]
[A, B, C]
[C, A, B]
[A, B]
->[A, C, B]
Consider the following code snippet. What type of exception will be thrown?
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] arr = new int[5];
System.out.println(arr[5]);
}
}
ArrayIndexOutOfBoundsException
NullPointerException
IndexOutOfBoundsException
ArithmeticException
->ArrayIndexOutOfBoundsException
import java.util.*;
class Animal {
void makeSound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.makeSound();
}
}
Animal makes sound
Dog barks
Compilation error
Runtime error
->Dog barks
->
Statement sta = conn.createStatement();
->executeUpdate()
->
To insert, update, or delete data in a database
->Query
Given the following code snippet, what will be the output?
String query = "SELECT name, age FROM Employees";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println(rs.getString("name") + " " + rs.getInt("age"));
}
Assume the Employees table has the following data:
| name | age |
| John | 30 |
| Jane | 25 |
John 30
Jane 25
name age
John 30
Jane 25
Jane 25
John 30
->
John 30
Jane 25
->
java.sql.Connection
Connection.getConnection(url)
Driver.getConnection(url)
DriverManager.getConnection(url)
new Connection(url)
->DriverManager.getConnection(url)
->setString()
set the maximum number of rows that a `Statement` object can return?
handle SQL warnings
check whether a `ResultSet` object contains more rows
->
check whether a `ResultSet` object contains more rows
jdbc:
jdbc,
db:
db,
->jdbc:
Conncetion
Statement
DriverManager
ResultSet
->DriverManager
->executeQuery()
->
None Of above
->conn.close()
Which method takes responsibility to check the type of request received from the
client and respond accordingly?
init()
service()
destroy()
request()
->service()
When a URL specific to a particular servlet is triggered, the _____________ method
is invoked.
init()
service()
destroy()
request()
->
service()
"init()": is called when the servlet is first loaded and initialized, not on every
request.
"service()": is the primary method that handles each incoming request to the
servlet.
"destroy()": is called when the servlet is being removed from the server and is
about to be unloaded.
________________ method is called by the Servlet when wanting to halt the current
or background threads
init()
service()
destroy()
request()
->destory()
Instantiation
Initialisation
Request Processing
Destroy
->Initialisation
->Request Processing
Instantiation:
This is the phase where a JSP page is first loaded and an instance of its servlet
class is created.
Initialization:
After instantiation, the jspInit() method is called to perform any necessary
initial setup.
Request Processing:
This is the phase where the actual processing of a client request happens, and the
_jspService() method is called to handle the request.
Destroy:
When the JSP page is being removed from the server, the jspDestroy() method is
called.
Which of the following code is used to get an attribute in a HTTP Session object in
servlets?
session.getAttribute(String name)
session.alterAttribute(String name)
session.updateAttribute(String name)
session.setAttribute(String name)
->session.getAttribute(String name)
->Welcome To Servlets
setAttribute()
addAttribute()
putAttribute()
setParam()
->setAttribute()
what the of following code snippet does? (Not sure)
<form action="submitServlet" method="post">
<input type="text" name="username" />
<input type="submit" value="Submit" />
</form>
->
<%! code %>
<include file="header.jsp"/>
<jsp:forward page="header.jsp"/>
<jsp:include>
<include page="header.jsp"/>
->
<jsp:include>
Consider the following code snippet. What will be the output if the servlet is
accessed with the URL /hello?
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
PrintWriter out = response.getWriter();
out.println("Hello, World!");
}
}
Hello, Servlet!
Hello, World!
Compilation error
Runtime error
->Hello, World
Consider the following code snippet in a JSP page. What will be the output?
<%! int count = 0; %>
<%= ++count %>
0
1
Compilation error
Runtime error
->1
In JSP, which tag is used to forward the request to another resource (such as
another JSP or servlet)?
<jsp:forward page="..."/>
<jsp:include page="..."/>
<%@ forward file="..."%>
<%! include file="..." %>
->
<jsp:forward page=""%>
Which tag is used to insert a Java expression directly into the output in a JSP
page?
->Class name can contain only letters and cannot have digits,underscore or dollar
sign.
The doGet() method in the example extracts values of the parameters type
and number by using
request.getParameter()
request.setParameter()
response.getParameter()
response.getAttribute()
->request.getParameter()
Which SQL constraint do we use to set some value to a field whose value has
not been added explicitly?
UNIQUE
Not Null
DEFAULT
CHECK
-> DEFAULT
->doGet()
JSP expressions begin with <%= ...%> tags and do not include semicolons.
True
False
->True
Which of the following statements are true for Final keword in java ?
Prevents method overriding
Prevents Inheritance
Once a variable is declared with final , it can be assigned value only once
None of the options
->
Prevents method overriding
Prevents Inheritance
Once a variable is declared with final , it can be assigned value only once
-> java.util
->False
How is a new record inserted into the database in JDBC with a parameterized
(dynamic) SQL query?
Using a Statement object
Using a ResultSet object
Using a PreparedStatement object
Using a Connection object
lass DemoClass {
static void test() throws RuntimeException {
throw new ArithmeticException();
}
public static void main (String args[]){
try {
test();
}
catch (RuntimeException re) {
}
}
}
->ExceptionHandled
SystemWriter
OutWriter
PrintWriter
None of the above
->PrintWriter
->Error
->response.setContentType("text/html")
In method overloading, methods must have the same name and different
signatures.
True
False
->True
Shreya has a Java application that needs to update the salary of an employee
with a specific ID in the database. Help Shreya in writing the SQL query
template that would she use with a PreparedStatement to achieve this ?
UPDATE employees SET salary = ? WHERE employee_id = ?;
MODIFY employees SET salary = ? WHERE employee_id = ?;
CHANGE employees SET salary = ? WHERE employee_id = ?;
ALTER employees SET salary = ? WHERE employee_id = ?;
What is the correct way to define a URL mapping for a Servlet using
annotations?
@WebServiet("/ServletPath”)
@URLMapping("/ServletPath")
@HttpServiet("/ServletPath”)
@RequestMapping("/ServletPath")
->@WebServiet("/ServletPath”)
What will happen if you try to get the value of a key that does not exist in a
“HashMap
It returns ‘null’.
It returns a default value if provided with 'getOrDefault’ method.
It throws an exception.
lt automatically adds the key to the map.
->executeQuery()
->Merge sort
Which file is created to configure the servlet container and servlet mappings
?
META-INF
web.xml
mapping.xml
WEB-INF
->web.xml
->DriverManager
In method overloading, methods must have the same name and different
signatures.
True
False
->True
->
/SampleServlet
*.do
Which of the following code is used to get a HTTP Session object in servlets
?
request.getSession()
response.getSession()
new Session()
None of the above
->request.getSession()