Lpu Cse310 Day 4 Ca3 Slot 2 Set 3
Lpu Cse310 Day 4 Ca3 Slot 2 Set 3
No. of Sections: 1
Total Questions: 15
Total Duration: 40 mins
No. of Questions: 15
Duration: 40 mins
Q1) Fill in the blanks to store a Character and a Float using a generic class. Output (for input A 4.5):
Grade: A, GPA: 4.5
Code Snippet
import java.util.Scanner;
Q2) Fill in the blanks to use a do-while loop to print values from 3 to 1. Output: 3 2 1
Code Snippet
public class Main {
public static void main(String[] args) {
int i = 3;
do {
System.out.println(i);
________;
} while(________);
}
}
Q3) Fill in the blanks to refer to outer class object from inner class. Output: Outer class reference is
working
Code Snippet
class Outer {
void ___________ {
System.out.println("Outer class reference is working");
}
class Inner {
void call() {
__________.test();
}
}
Q4) Fill in the blanks to override a method in the child class. Output: Child speaking
Code Snippet
class Parent {
void speak() {
System.out.println("Parent speaking");
}
}
Q5) Identify the correct replacement for the marked issue so that the program prints "Child class
inherits from Parent"
Code Snippet
class Parent {
void msg() {
System.out.println("Child class inherits from Parent");
}
}
Options
1) c.msg();
2) c.message();
3) c.say();
4) c.print();
Q6) Identify the correct replacement for the marked issue so that the program prints "Cold"
Code Snippet
class Cart {
public static void main(String[] args) {
String season = "Winter";
if (season == "Winter") { // Issue
System.out.println("Hot");
} else {
System.out.println("Cold");
}
}
}
Options
1) if (season = "Winter")
2) if (season != "Winter")
3) if (season ! "Winter")
4) if (season =! "Winter")
Q7) Find the correct replacement for the marked issue so that program print only even numbers
between 2 and 10.
Code Snippet
public class Main {
public static void main(String[] args) {
for (int i = 2; i <= 10; i + 2) // Issue
{
System.out.println(i);
}
}
}
Options
1) i += 1
2) i++
3) i += 2
4) i = i + 3
Options
1) sb.append("World");
2) sb.add("World");
3) append(sb, "World");
4) sb.concat("World");
Q9) Arrange the code to demonstrate a bounded generic type with Number class.
Code Snippet
class Main {
1. }
2. <T extends Number> void printSum(T num1, T num2) {
3. System.out.println("Sum: " + (num1.doubleValue() + num2.doubleValue()));
4. public static void main(String[] args) {
5. Main obj = new Main();
6. obj.printSum(5, 10);
7. }
}
Options
1) 2 3 4 1 5 6 7
2) 1 2 3 4 5 6 7
3) 2 3 1 4 5 6 7
4) 2 3 1 4 6 5 7
Q10) Arrange the code to access a parent class method from a child class object.
Code Snippet
1. class Bike extends Vehicle {
2. b.start();
3. void start() {
4. System.out.println("Vehicle started");
5. Bike b = new Bike();
6. public class Main {
7. public static void main(String[] args) {
8. class Vehicle {
}
}
}
}
}
Options
1) 8 3 4 1 6 7 5 2
2) 8 3 4 1 6 7 2 5
3) 1 8 3 4 6 7 5 2
4) 6 1 5 2 3 4 8 7
Q11) Arrange the following lines of code to calculate the number of days between two dates
Code Snippet
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DaysBetweenDates {
1. LocalDate endDate = LocalDate.of(2023, 5, 15);
2. public static void main(String[] args) {
3. System.out.println("Start date: " + startDate);
4. LocalDate startDate = LocalDate.of(2023, 3, 10);
5. System.out.println("End date: " + endDate);
6. long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
7. System.out.println("Number of days between: " + daysBetween);
}
}
Options
1. 2431567
2. 2417563
3. 2143567
4. 4123567
Options
1) 427935681
2) 568142793
3) 429375681
4) 742569381
Q13) Which of the following programs correctly uses the continue statement in a loop?
A.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.print(i + " ");
}
}
}
B.
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
i++;
if (i == 3) continue;
System.out.print(i + " ");
}
}
}
C.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
continue;
}
System.out.print(i + " ");
}
}
}
D.
public class Main {
public static void main(String[] args) {
int i = 0;
do {
if (i == 2) continue;
System.out.print(i + " ");
i++;
} while (i < 5);
}
}
Options
1) A, B, D
2) A, C, D
3) B, D
4) A, B, C
Q14) Which of the following Java programs correctly use wrapper classes for autoboxing and
unboxing?
A.
public class WrapperTest {
public static void main(String[] args) {
Integer i = 10;
int a = i;
System.out.println(a);
}
}
B.
public class WrapperTest {
public static void main(String[] args) {
Double d = 12.5;
double b = d;
System.out.println(b);
}
}
C.
public class WrapperTest {
public static void main(String[] args) {
Integer i = 5;
int j = i;
String s = i.toString();
System.out.println(s);
System.out.println(j);
}
}
D.
public class WrapperTest {
public static void main(String[] args) {
Character c = 'A';
char ch = c;
System.out.println(c);
System.out.println(ch);
}
}
Options
1) A, B, C, D
2) A, B, D
3) A, C, D
4) B, C, D
Q15) Which of the following Java programs will compile and print Java?
A.
public class SwitchTest {
public static void main(String[] args) {
String language = "Java";
switch(language) {
case "Java":
System.out.println("Java");
break;
case "C":
System.out.println("C");
break;
default:
System.out.println("Unknown");
}
}
}
B.
public class SwitchTest {
public static void main(String[] args) {
String language = "Python";
switch(language) {
case "Java":
System.out.println("Java");
break;
default:
System.out.println("Unknown");
}
}
}
C.
public class SwitchTest {
public static void main(String[] args) {
String language = "Java";
switch(language) {
case "Java":
System.out.println("Java");
case "C":
System.out.println("C");
break;
default:
System.out.println("Unknown");
}
}
}
D.
public class SwitchTest {
public static void main(String[] args) {
String language = "Java";
switch(language) {
case "Java":
System.out.println("Java");
break;
case "Python":
System.out.println("Python");
break;
}
}
}
Options
1) A, D
2) B, C
3) A, C
4) B, D