0% found this document useful (0 votes)
2 views8 pages

Lpu Cse310 Day 4 Ca3 Slot 2 Set 3

The document outlines a code-based test consisting of 15 questions, each requiring the completion of code snippets or the identification of correct code replacements. The test covers various programming concepts, including generics, loops, method overriding, and the use of wrapper classes. Each question provides a specific output requirement and options for completion.

Uploaded by

amerinder.66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Lpu Cse310 Day 4 Ca3 Slot 2 Set 3

The document outlines a code-based test consisting of 15 questions, each requiring the completion of code snippets or the identification of correct code replacements. The test covers various programming concepts, including generics, loops, method overriding, and the use of wrapper classes. Each question provides a specific output requirement and options for completion.

Uploaded by

amerinder.66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Test Summary

No. of Sections: 1
Total Questions: 15
Total Duration: 40 mins

Section 1 - Code Based Test 3


Section Summary

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;

class Info<T, U> {


T x;
U y;
Info(T x, U y) {
this.x = x;
this.y = y;
}
void display() {
System.out.println("Grade: " + x + ", GPA: " + y);
}
}

public class Main {


public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
char grade = scn.next().charAt(0);
float gpa = scn.nextFloat();
Info<___> obj = new ___(grade, gpa);
obj.___();
}
}

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();
}
}

public static void main(String[] args) {


Outer outer = new Outer();
Outer.Inner in = outer.new Inner();
in.call();
}
}

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");
}
}

class Child extends Parent {


@Override
________ {
System.out.println("Child speaking");
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child();
c.________;
}
}

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");
}
}

class Child extends Parent {}

public class Test {


public static void main(String[] args) {
Child c = new Child();
c.m(); // issue
}
}

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

Q8) Fix the line to print: JavaWorld


Code Snippet
class JoinBuilder {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java");
sb.append = "World"; // Error
System.out.println(sb);
}
}

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

Q12) Arrange the code to define and implement an interface.


Code Snippet
1. obj.sayHello();
2. void sayHello();
3. System.out.println("Hello from implementation");
4. interface Greet {
5. public class Main {
6. public static void main(String[] args) {
7. class Hello implements Greet {
8. Greet obj = new Hello();
9. public void sayHello() {
}
}
}
}
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy