0% found this document useful (0 votes)
3 views9 pages

LPU Java Paper Mock

The document outlines a code-based test consisting of 15 questions divided into three sections: fill-ups, issue code, and multiple correct answers. Each section tests various programming concepts including type promotion, operators, loops, encapsulation, nested loops, and the ternary operator. The total duration for the test is 40 minutes, with specific questions designed to assess coding skills and error identification.
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)
3 views9 pages

LPU Java Paper Mock

The document outlines a code-based test consisting of 15 questions divided into three sections: fill-ups, issue code, and multiple correct answers. Each section tests various programming concepts including type promotion, operators, loops, encapsulation, nested loops, and the ternary operator. The total duration for the test is 40 minutes, with specific questions designed to assess coding skills and error identification.
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

Test Summary

No. of Sections: 1
Total Questions: 15
Total Duration: 40 mins
Section 1- Code Based Test

Section Summary

No. of Questions: 8
Duration: 40 mins

Fill-ups (4 Questions)

Q1. What should replace the blank for type promotion?

public class TypePromotion {


public static void main(String[] args) {
byte b = 10;
int i = 20;
______ result = b + i;
[Link](result);
}
}

a) byte
b) short
c) int
d) long

Q2. What operator should replace the blank for less than?

public class LessThan {


public static void main(String[] args) {
int a = 5, b = 10;
boolean less = a ______ b;
[Link](less);
}
}

a) <
b) >
c) <=
d) >=

Q3. What should replace the blank to print numbers 1 to 5 using while loop?

public class WhileLoop {


public static void main(String[] args) {
int i = 1;
while(i ______ 5) {
[Link](i);
i++;
}
}
}

a) >
b) <
c) <=
d) ==

Q4. What should replace the blank for constructor overloading?

public class Circle {


double radius;

public Circle() {
radius = 1.0;
}

public ______(double r) {
radius = r;
}

public static void main(String[] args) {


Circle c1 = new Circle();
Circle c2 = new Circle(5.0);
}
}
a) Circle
b) Constructor
c) initCircle
d) CircleConstructor

Issue Code (4 Questions)


Q5) Identify the correct code after fixing the error to get the expected output: "Initial: 0"
public class DefaultValues {
static int count;
public static void main(String[] args) {
int value; // Error 1
[Link]("Initial: " + value);
}
}

a) int value = 0;
b) static int value;
c) Integer value = 0;
d) int value = count;

Q6) Identify the correct code after fixing the error to get the expected output: "Number is 5"
public class IfAssignment {
public static void main(String[] args) {
int number = 5;
if (number = 5) { // Error 1
[Link]("Number is 5");
}
}
}
a) if (number == 5)
b) if (number + 5)
c) if (!number)
d) if ([Link](5))

Q7) The code throws a NullPointerException at runtime. Which correction fixes the
error in line 8?
public class ObjectArray {
public static void main(String[] args) {
String[] names = new String[3];
names[0] = "Alice";
names[1] = "Bob";

for (int i = 0; i < [Link]; i++) {


[Link](names[i].toUpperCase()); // Error
}
}
}

a) if (names[i] != null) [Link](names[i].toUpperCase());


b) [Link](names[i].toString().toUpperCase());
c) [Link](names[i].toLowerCase().toUpperCase());
d) [Link](names[j].toUpperCase());

Q8) The code doesn't remove spaces from the original string. Which correction fixes
the error in line 4?
public class StringTrim {
public static void main(String[] args) {
String str = " Hello World ";
[Link](); //Error
[Link]("[" + str + "]");

String trimmed = [Link]();


[Link]("[" + trimmed + "]");
}
}

a) str = [Link]();
b) [Link]().toString();
c) String newStr = [Link]();
d) str = str;

Multiple Correct (3 Questions)


Q9) Which snippets correctly demonstrate encapsulation?

a) public class BankAccount { private double balance;

public double getBalance() {

return balance;

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

public boolean withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

return true;

return false;

}
b) public class Student {

public String name; // public field - poor encapsulation

public int age;

c) public class Rectangle { private double width; private double height;

public Rectangle(double width, double height) {

setWidth(width);

setHeight(height);

public void setWidth(double width) {

if (width > 0) [Link] = width;

public void setHeight(double height) {

if (height > 0) [Link] = height;

}
d) public class Person {

private String ssn; // Social Security Number

public String getSSN() { // exposes sensitive data

return ssn;

Options:

1) a,c

2) a,b,d

3) b,c,d

4) a,b,c

Q10) Which snippets correctly demonstrate nested loop patterns?

a) for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { [Link]("*"); }


[Link](); }

b) for (int i = 5; i >= 1; i--) {


for (int j = 1; j <= i; j++) {
[Link](j + " ");
}
[Link]();
}
c) int rows = 3, cols = 4; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++)
{ [Link]("(" + i + "," + j + ") "); } [Link](); }

d)
for (int i = 0, j = 0; i < 3 && j < 3; i++, j++) {
[Link](i + " " + j);
}

Options:

1) a,b,c

2) a,b,d

3) b,c,d

4)a,b,c,d

Q11) Which snippets correctly demonstrate ternary operator?

a) int a = 10, b = 20;


int max = (a > b) ? a : b;
String result = (a > b) ? "a larger" : "b larger or equal";

b) int score = 85;


String grade = (score >= 90) ? "A" :
(score >= 80) ? "B" :
(score >= 70) ? "C" :
(score >= 60) ? "D" : "F";

c) String text = null;


String output = (text != null) ? [Link]() : "DEFAULT";

d) int x = 5;
int y = (x > 0) ? "positive" : "negative"; // type mismatch

Options:

1) a,b,c

2) a,b,d
3) b,c,d

4) a,b,c,d

You might also like