PRA1 JAVA 20june2024 Pune With Answers 1
PRA1 JAVA 20june2024 Pune With Answers 1
Adv Java 1
Single
Multiple
Multilevel
Hierarchical
Adv Java 2
package
class
library
interface
Adv Java 3
1/28
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
Adv Java 4
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
Adv Java 5
2/28
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
Adv Java 6
Car started
Bike started
Bike started
Car started
Compilation error
3/28
Adv Java 7
Adv Java 7
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);
Adv Java 8
4/28
Adv Java 9
Adv Java 9
What will be the output for following code Snippet ? public class Demo {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> list2 = new ArrayList<>(Arrays.asList(4, 5, 6, 7, 8));
list1.retainAll(list2);
System.out.println(list1);
}
}
[4,5]
[1,2,3,4,5]
[4,5,4,5]
[6,7,8]
Adv Java 10
{null=Fest} -- answer
{null=Test}
Adv Java 11
class Animal {
void eat() {
5/28
System.out.println("Eating");
}
}
Eating
Bird eating
Compilation Error
Runtime Error
Adv Java 12
First
Second
Third
Zero
Adv Java 13
try {
6/28
int[] arr = new int[2];
arr[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException caught");
} finally {
System.out.println("Finally block executed");
}
ArrayIndexOutOfBoundsException caught
ArrayIndexOutOfBoundsException caught
Finally block executed
Compilation error
Adv Java 14
Adv Java 15
class Parent {
void display() {
7/28
System.out.println("Parent");
}
}
Parent
Compilation Error
Runtime Error
Child
Advance_Java
[A, C, B]
[A, B, C]
[C, A, B]
[A, B]
Advance_Java
8/28
The exception will be handled by the JVM and the program will continue.
Advance_Java
Consider the following code snippet. What type of exception will be thrown?
int[] arr = new int[5];
System.out.println(arr[5]);
ArrayIndexOutOfBoundsException
NullPointerException
IndexOutOfBoundsException
ArithmeticException
Advance_Java
class Animal {
void makeSound() {
System.out.println("Animal makes sound");
}
}
Dog barks
Compilation error
Runtime error
The ability of different classes to be treated as instances of the same class through inheritance.
The ability to write a class that can be used with any data type.
JDBC 1
Which among the following is the correct way to create a Statement object "sta" inside the Connection object "conn"?
JDBC 2
executeResult()
executeQuery()
executeUpdate()
execute()
JDBC 4
JDBC 5
Which method in JDBC is used to execute an SQL query and retrieve the result as a ResultSet object?
executeQuery()
executeUpdate()
execute()
executeResult()
JDBC 6
11/28
Update
Select
Query
JDBC 7
John 30
Jane 25
name age
John 30
Jane 25
Jane 25
John 30
JDBC 8
12/28
JDBC 9
java.sql.Connection
java.sql.Statement
java.sql.ResultSet
java.sql.Transaction
JDBC 10
Connection.getConnection(url)
Driver.getConnection(url)
DriverManager.getConnection(url)
new Connection(url)
JDBC 11
setParameter()
setValue()
setString()
setText()
JDBC 12
if (rs.next()) {
13/28
// Process the row
}
set the maximum number of rows that a `Statement` object can return?
JDBC 13
try {
Connection connection = DriverManager.getConnection("jdbc:derby: :C//MyDB;create=true");
System.out.println("Connected to the database successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
JDBC 14
jdbc:
jdbc,
db:
db,
JDBC 15
14/28
Which class is used to establish a connection to a database in JDBC?
Conncetion
Statement
DriverManager
ResultSet
JDBC
JDBC
executeUpdate()
executeQuery()
execute()
executeResultSet()
JDBC
15/28
It closes the database connection.
JDBC
JDBC
conn.close()
conn.disconnect()
conn.terminate()
conn.shutdown()
Java Web 1
Which method takes responsibility to check the type of request received from the client and respond accordingly?
init()
service()
destroy()
request()
16/28
Question - 42 SCORE: 5 points
Java Web 2
Java Web 2
When a URL specific to a particular servlet is triggered, the _____________ method is invoked.
init()
service()
destroy()
request()
Java Web 3
________________ method is called by the Servlet when wanting to halt the current or background threads
init()
service()
destroy()
request()
Java Web 4
Instantiation
Initialisation
Request Processing
Destroy
Java Web 5
17/28
_jspservice() method is invoked in which phase of the JSP Life cycle?
Instantiation
Initialisation
Request Processing
Destroy
Java Web 6
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)
Java Web 7
20
15
2.02E+0.11
Java Web 9
18/28
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Out.println("<h2>Welcome to Servlets</h2>");
}
What will be displayed when this Servlet is accessed through a browser?
An Error message
Welcome
Welcome To Servlets
Compilation error
Java Web 10
Java Web 11
setAttribute()
addAttribute()
putAttribute()
setParam()
Java Web 12
19/28
what the of following code snippet does?
Java Web 13
Java Web 8
20/28
Java Web 14
<include file="header.jsp"/>
<jsp:forward page="header.jsp"/>
<jsp:include>
<include page="header.jsp"/>
Java Web 15
<scriptlet>
<jsp:scriptlet>
<%-- --%>
<% %>
JavaWeb
.jsp
.html
.java
.xml
JavaWeb
Consider the following code snippet. What will be the output if the servlet is accessed with the URL /hello?
21/28
@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
JavaWeb
Consider the following code snippet in a JSP page. What will be the output?
<%! int count = 0; %>
<%= ++count %>
Compilation error
Runtime error
JavaWeb
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="..."/>
JavaWeb
22/28
Which tag is used to insert a Java expression directly into the output in a JSP page?
<% %>
<%= %>
<%! %>
You are required to manipulate an ArrayList of integers using an Iterator. Write a program that performs the following tasks:
Add several integers to the ArrayList.
Use the Iterator to remove all even numbers from the list.
Sample Input 1 :
Initial ArrayList: [1, 2, 3, 4, 5, 6]
Sample Output 1 :
After removing even numbers, the expected ArrayList should be: [1, 3, 5]
Advanced_Java
Sample Input 1
5
10
Sameeksha
5
Ritin
Sameeksha
Sameeksha
Sample Output1
2
Sample Input 2
3
1
Rekha
114
Seema
Sample Output2
Rekha
114
1
23/28
Question - 63 SCORE: 30 points
Advance Java - Flight
Java - Flight
NOTE: ANY MEANS OF INTENDED PLAGIARISM WHILE ATTEMPTING THIS QUESTION IS LIABLE TO HIGHEST POSSIBLE STRICT HR ACTIONS AS IT IS
VIOLATION OF INTEGRITY. HENCE, REFRAIN FROM ANY SUCH MEANS AND ATTEMPT THIS QUESTION AS PER YOUR EXPERTISE ONLY.
flightNumber : int
origin : String
destination : String
distance : double ( in miles)
pricePerMile : double (in dollars)
Write getters, setters and parameterized constructor in the above-mentioned attribute sequence as required.
findTotalPriceOfFlightByFlightNumber
Create a static method findTotalPriceOfFlightByFlightNumber in the Solution class. This method will take two parameters, an array of Flight objects and
one int parameter (flight number) and return the total price of flight of mentioned flightNumber. If no flights are present in the array of Flight objects with
mentioned flight number , then the method should return zero.
findFlightWithSecondLowestPricePerMile
Create a static method findFlightWithSecondLowestPricePerMilein the Solution class. This method will take an array of Flight objects and return the flight
object with the second lowest price .If no flights are present with the second lowest price per mile, then the method should return null.
1. Take the necessary input variable and call findTotalPriceOfFlightByFlightNumber. For this method - The main method should print the total price of
flight with matching flight number given if the returned value is not 0, or it should print “No Flight found for mentioned flight number.”
2. Take the necessary input variable and call findFlightWithSecondLowestPricePerMile. For this method - The main method should print the flight details
of the flight object with second lowest price per mile if the returned value is not null, or it should print "No Flight found"
Note:
The above-mentioned static methods should be called from the main method. Also write the code for accepting the inputs and printing the outputs. Don't
use any static test or formatting for printing the result.
24/28
You can use/refer to the below given sample input and output to verify your solution.
The 1st input taken in the main section is the number of flight objects to be added to the list of Flights.
The next set of inputs are flightNumber,origin,destination,distance( in miles),pricePerMile(in dollars)for each Flight object taken one after another and is
repeated for the number of Flights objects given in the first line of input.
Sample input - 1
4
1001
New York
Los Angeles
2450
0.15
1002
London
Paris
215
0.25
1003
Tokyo
Sydney
4900
0.18
1004
Toronto
Vancouver
2100
0.22
1003
Sample Output - 1
Sample Input - 2
5
1005
Dubai
Istanbul
2400
0.20
1006
Cape Town
Nairobi
3400
0.30
1007
Moscow
Berlin
25/28
1200
0.12
1008
Buenos Aires
Rio de Janeiro
1000
0.16
1009
Sydney
Auckland
1300
0.17
1006
Sample Output - 2
CORE JAVA
eventId-int
eventname-String
location-String
eventfee-int
eventAttendee-int
The above attributes should be private. Write getters, setters and parameterized constructor as required.
findCountOfEventByAttendee method:
This method will take array of Event objects and an integer value as input parameters.This method will return the count of event from an array of event
objects for the given eventAttendee (int parameter passed).
If no event with the given quantity is present in the array of event objects,then the method should return 0.
getLowestEventByPrice method:
This method will take array of object and an String value as input parameter.This method will return the event object with lowestPrice for the given
location.if no Product with the above condition present in the array of object then the method should return null.
The above-mentioned static method should be called from the main method.
26/28
For findCountOfEventLocation method - The main method should print the returned count as it is if the returned value is greater than 0 or it should print
"No such event found.".
For getLowestEventByPrice method - The main method should print the eventname,location and fee from the returned event object if the returned value is
not null. If the returned value is null then it should print "No such event found.".
Before calling these static methods in main, use Scanner object to read the values of four event objects referring to attributes in the above-mentioned
attribute sequence.
Next, read the value of the int parameter and a String parameter for capturing eventAttendee and event fees respectively.
Test case1:
101
Marriage
Mumbai
40000
200
102
Birthday
kolkata
15000
100
103
MARRIAGE
Delhi
19000
200
104
marriage
mumbai
35000
150
200
mumbai
Output:
2
marriage
mumbai
35000
Test case2:
101
Marriage
Mumbai
40000
200
102
Birthday
kolkata
15000
100
103
MARRIAGE
Delhi
19000
200
27/28
104
marriage
mumbai
35000
150
300
pune
Output:
No such event found.
No such event found.
28/28