0% found this document useful (0 votes)
17 views

Java ST3 B2021 SEM4

This document contains a practice exam for the Java-ST3-B2021-SEM4-CSEAI exam. It includes 14 multiple choice questions related to Java concepts like threads, exceptions, strings, classes, and arrays. For each question, it provides the code snippet or scenario being tested and the correct answer.

Uploaded by

Akshansh Mahajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java ST3 B2021 SEM4

This document contains a practice exam for the Java-ST3-B2021-SEM4-CSEAI exam. It includes 14 multiple choice questions related to Java concepts like threads, exceptions, strings, classes, and arrays. For each question, it provides the code snippet or scenario being tested and the correct answer.

Uploaded by

Akshansh Mahajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

CODEQUOTIENT

PAPER OF JAVA-ST3-B2021-SEM4-CSEAI EXAM

Q1. thread

What is the output of the following Program?

1. class priority_thread
2. {
3. public static void main(String args[])
4. {
5. Thread p = Thread.currentThread();
6. System.out.println(p);
7. }
8. }

0
1
4
5

Ans : 5

Q2. What will be the output of the following source code in Java

public class Example1

{ public static void main(String args[])

{ int num1, num2;

try {

num1 = 2;

num2 = 40 / num1;

1 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

System.out.println(num2);

System.out.println("You need to have a burning desire in you to study. ");

catch (ArithmeticException e)

System.out.println("The secret behind getting ahead is getting started. ");

catch (Exception e)

System.out.println("To get at the first position, you have to beat the current best strategy. ");

System.out.println("There is nothing better than a stable and focused mind.");

}}

Infinite Value generated The secret behind getting ahead is getting started. There is nothing better than a
stable and focused mind.
20 You need to have a burning desire in you to study
20 You need to have a burning desire in you to study. There is nothing better than a stable and focused
mind
The secret behind getting ahead is getting started. Infinite Value generated There is nothing better than a
stable and focused mind You need to have a burning desire in you to study. There is nothing better than a
stable and focused mind

Ans : 20 You need to have a burning desire in you to study. There is nothing better than a stable and focused
mind

Q3. Strings

What will be the output of the following code ?

1. class myCode
2. {

2 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

3. public static void main(String args[])


4. {
5. String st = "true false true";
6. boolean bool = Boolean.valueOf(st);
7. System.out.print(bool);
8. }
9. }

true
Runtime Error
false
Compilation Error

Ans : false

Q4. Predict the output

class A

protected A(String s)

System.out.println("hello");

public class Main extends A

public static void main(String arg[])

Main o=new Main("hi");

private Main(String s)

super(s);

System.out.println(s);

3 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

hello hi
hi hi
hi hello
compile time error

Ans : hello hi

Q5. MCQ-6

Which of the following is true?

The Class class is the superclass of the Object class.


The Object class is final.
The Class objects are constructed by the JVM as classes are loaded by an instance of
java.lang.ClassLoader
None of the above.

Ans : The Class objects are constructed by the JVM as classes are loaded by an instance of
java.lang.ClassLoader

Q6. MCQ-1-Marks

What will be the output of the following Java program?


class variable_scope
{
public static void main(String args[])
{
int x;
x = 5;
{
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}

Compilation error
Runtime error
5656
565

Ans : Compilation error

4 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Q7. MCQ-1-Marks

Which one is a valid declaration of a boolean?

boolean b1 = 1;
boolean b2 = ‘false’;
boolean b2 = false;
boolean b4 = ‘true’

Ans : boolean b2 = false;

Q8. MCQ-1-Marks

What will be the output of the following Java code?


class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}

3
0
6
1

Ans : 1

Q9. MCQ-1-Marks

What will be the output of the following Java code?


class Output
{
public static void main(String args[])
{
int x=y=z=20;

}
}

5 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

compile and runs fine


20
run time error
compile time error

Ans : compile time error

Q10. MCQ-1-Marks

Which of these is necessary condition for automatic type conversion in Java?

The destination type is smaller than source type


The destination type is larger than source type
The destination type can be larger or smaller than source type
None of the mentioned

Ans : The destination type is larger than source type

Q11. MCQ-1-Marks

What will this code print?

int arr[] = new int [5];


System.out.print(arr);

0
value stored in arr[0].
00000
Class name@ hashcode in hexadecimal form

Ans : Class name@ hashcode in hexadecimal form

Q12. MCQ-1-Marks

10. How many copies of static and class variables are created when 10 objects are created of a class?

1, 10
10, 10

6 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

10, 1
1, 1

Ans : 1, 10

Q13. Choose the correct option

Given the following definition of the class

class Main{

public static void main(String[] args) {

System.out.println(args[1]+":"+ args[2]+":"+ args[3]);

what is the output of Main if it is executed using the following command?

java Main one two three four

one:two:three
Main:one:two
java:EJavaGuru:one
two:three:four

Ans : two:three:four

Q14. Choose the correct option

select the correct option

What is the output of the below Java program?

int balls[], rounds=3;

balls = new int[rounds];

for(int i=0; i<balls.length; i++)

_____________________//1

for(int j=0; j<balls.length; j++)

7 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

System.out.print(balls[j] + ",");

Which option when written in position 1 will display 2 4 6

balls[i] = (i+1)*2;
balls[i] = (i*2);
balls[i] = (i+2)-2;
balls[i] = (i+2)*2;

Ans : balls[i] = (i+1)*2;

Q15. Choose the correct option

Predict the output

public class Main

public static void main(String[] args) {

int num = 120;

switch (num) {

default: System.out.println("default");

case 0: System.out.println("case1");

case 100*1+20: System.out.println("case2");

break;

defaultcase1case2
case2
case1
case1case2

8 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Ans : case2

Q16. Choose the correct option

Which package is imported into every Java class by default?

java.util
java.lang
system.lang
java.system

Ans : java.lang

Q17. Choose the correct option

Predict the output of the following pseudo code

public class Main

public static void main(String[] args) {

int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};

int n = 6;

n = arr[arr[n] / 2];

_____________ //4

Given the following code what will printed if we print the following expression

++arr[n]/2 in the 4 th line

3
1
5
9 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Ans : 2

Q18. Compile and Print

Given the following code -


public class MyFirstClass{
public static void main(String[] args){
System.out.println(args[1]);
}
}

Which of the following commands will compile and then print "hello"?

javac MyFirstClass java MyFirstClass hello hello


javac MyFirstClass.java java MyFirstClass hello hello
javac MyFirstClass java MyFirstClass hello
javac MyFirstClass.java java MyFirstClass hello

Ans : javac MyFirstClass.java java MyFirstClass hello hello

Q19. Exception

For arithmetic exception condition _________ is thrown.

Arithmetic exception
Mathematical exception
All
Numerical exception

Ans : Arithmetic exception

Q20. PHONE BOOK

User has to create a phone book and later on which can be used to query. The phone book contains
entries consists of people's names and their phone number. After that you will be given some person's
name as query. For each query, print the phone number of that person.

10 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Input Format

The first line will have an integer denoting the number of entries in the phone book. Each entry
consists of two lines: a name and the corresponding phone number.

After these, there will be some queries. Each query will contain a person's name. Read the queries until
end-of-file.

Output Format

For each case, print "Not found" if the person has no entry in the phone book. Otherwise, print the
person's name and phone number. See sample output for the exact format.

Code Limits

Given that: Phone number is 5 digit long.

Sample Input

3 // number of inputs

Ram //name

28888 //phone number

Gautam

22222

Sham

33333

2 // number of quries

Sham //1st query contains person name

Ravi //2nd query contains person name

11 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Sample Output

Sham=33333

Not found

ANSWER:

Test case 1 :
Input:
3
Ram
28888
Gautam
22222
Sham
33333
2
Sham
Ravi
Output:
Sham=33333
Not found

Test case 2 :
Input:
1
Ankush
98765
1
Aniket
Output:
Not found

Test case 3 :
Input:
3
vinay
22223
gautam
22222
ram
23232
1
ram
Output:
ram=23232

Test case 4 :

12 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Input:
1
Deepak
12345
2
Deepak
deepak
Output:
Deepak=12345
Not found

Test case 5 :
Input:
4
A
11111
B
22222
C
33333
D
44444
3
D
A
B
Output:
D=44444
A=11111
B=22222

Test case 6 :
Input:
5
AAAAA
11111
AAAAB
22222
AAAAC
33333
AAAAD
44444
AAAAE
55555
2
AAAAB
AAAAE
Output:
AAAAB=22222
AAAAE=55555

13 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Q21. Sorting of Student record based on ID

You are given a list of student information: ID, First Name, and CGPA. Your task is to rearrange them
according to their ID in the non-decreasing order. No two students have the same ID.

Input Format

The first line of input contains an integer, representing the total number of students. The next lines
contains a list of student information in the following structure:

ID Name CGPA

The name contains only lowercase English letters. The ID contains only integer numbers without
leading zeros. The CGPA will contain, at most, 2 digits after the decimal point.

Output Format

After rearranging the students according to the above rules, print the first name of each student on a
separate line.

Sample Input

33 Rumpa 3.68

85 Ashis 3.85

56 Samiha 3.75

19 Samara 3.75

22 Fahim 3.76

Sample Output

Samara

Fahim

Rumpa

Samiha

Ashis

14 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

ANSWER:

Test case 1 :
Input:
5
33 Rumpa 3.68
85 Ashis 3.85
56 Samiha 3.75
19 Samara 3.75
22 Fahim 3.76
Output:
Samara
Fahim
Rumpa
Samiha
Ashis

Test case 2 :
Input:
2
1 Aman 1.2
3 Sunil 2.3
Output:
Aman
Sunil

Test case 3 :
Input:
1
23 Suman 12
Output:
Suman

Test case 4 :
Input:
7
31 Rumpa 3.68
8 Ashis 3.85
5 aman 3.75
9 Samara 3.75
2 aman 3.76
6 sunil 2.2
1 aman 1.2
Output:
aman
aman
aman
sunil
Ashis
Samara
Rumpa

15 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Test case 5 :
Input:
6
31 Rumpa 3.68
8 Ashis 3.85
5 Samiha 3.75
9 Samara 3.75
2 Fahim 3.76
6 sunil 2.2
Output:
Fahim
Samiha
sunil
Ashis
Samara
Rumpa

Test case 6 :
Input:
4
31 Rumpa 3.68
8 Ashis 3.85
5 Kunal 3.75
9 Ravi 3.75
Output:
Kunal
Ashis
Ravi
Rumpa

Q22. Write a Java program in which you need to extend the abstract class in different class like
Rectangle, Circle and Triangle.

You are given a abstract class Shape in which there are two methods are given named display() and
calculateArea(). Write a Java program in which you need to extend the abstract class in different class
like Rectangle, Circle and Triangle.

In the abstract class, the method display() is used to display the area and calculateArea() is used to
calculate the area.

Sample Case:

Input:

3 //Length of the Rectangle

4 //Breadth of the Rectangle

16 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

4 //Radius of the Circle

2 //Base of the Triangle

3 //Height of the Triangle

Output:

Area of the Rectangle is = 12

Area of the Circle is = 50.24

Area of the Triangle is = 3.0

ANSWER:

Test case 1 :
Input:
1
2
3
4
5
Output:
Area of the Rectangle is = 2
Area of the Circle is = 28.259999999999998
Area of the Triangle is = 10.0

Test case 2 :
Input:
3
4
5
6
7
Output:
Area of the Rectangle is = 12
Area of the Circle is = 78.5
Area of the Triangle is = 21.0

Test case 3 :
Input:
1
2
4
6
8
Output:
Area of the Rectangle is = 2
Area of the Circle is = 50.24
Area of the Triangle is = 24.0

17 of 18 19-05-2023, 07:27 pm
Download | CodeQuotient https://test.codequotient.com/test/getTestContentFile/6454c523f7acf33...

Test case 4 :
Input:
2
4
6
4
3
Output:
Area of the Rectangle is = 8
Area of the Circle is = 113.03999999999999
Area of the Triangle is = 6.0

Test case 5 :
Input:
3
4
4
2
3
Output:
Area of the Rectangle is = 12
Area of the Circle is = 50.24
Area of the Triangle is = 3.0

18 of 18 19-05-2023, 07:27 pm

You might also like