Object Oriented Programming using Java, OOPJ (C2311C1) · Continuous Evaluation Test CET-1 · 20 August 2025 · 30 marks · 10:30 am – 12:00 pm ITM (SLS) Baroda University · SOCSET · B.Tech batch 2024-28 (the header prints "Semester I"; the course is in Semester 3). Questions transcribed from the original paper. Solutions are written by CampusLoop; check them against your class notes. Programs were compiled and run on JDK 26.
Q1. Multiple choice questions [5]
1. Which of the following is not a feature of Java? a) Platform independent · b) Pointer support · c) Object oriented · d) Secure Answer: (b) Pointer support.
2. What is the result of int x = 10; System.out.println(x++ + ++x);? a) 21 · b) 22 · c) 20 · d) 23
Answer: (b) 22. x++ gives 10 (then x = 11); ++x makes x = 12 and gives 12; 10 + 12 = 22.
3. What is the role of the JVM? a) Converts source code into bytecode · b) Provides an environment to run bytecode · c) Acts as a text editor · d) Provides debugging support only Answer: (b). (javac converts source into bytecode.)
4. Which operator is used to concatenate strings in Java? a) . · b) + · c) & · d) concat
Answer: (b) +. (concat() is a method, not an operator.)
5. If you do not explicitly define any constructor: a) No constructor is available · b) Compiler automatically provides a default constructor · c) Program will not compile · d) JVM creates a parameterized constructor Answer: (b).
Q2. Attempt any two out of four [10]
Q2.1 Differentiate between JDK, JVM and JRE with a diagram.
JDK = JRE + development tools (javac, jar, javadoc); JRE = JVM + class libraries; JVM loads, verifies and executes bytecode. Diagram and table: Java end-sem 2025, Q2.4.
Q2.2 Explain the concept of object reference in Java.
A reference variable stores the address of an object on the heap; copying a reference shares the same object; null refers to nothing. Full answer with program: Java end-sem 2025, Q2.5.
Q2.3 Command-line arguments: print sum, difference, product and quotient.
Parse args[0] and args[1] with Integer.parseInt, check for division by zero. Program: Java end-sem 2025, Q4.6(a).
Q2.4 Can we overload the main method? How is the overloaded main accessed?
Yes; the JVM only starts from main(String[] args), and the other versions must be called explicitly. Program and output: Java end-sem 2025, Q4.2.
Q3. Attempt any two out of four [10]
Q3.1 Explain the use of wrapper classes with an example.
Wrapper classes (Integer, Double, Character, …) let primitives be used as objects in collections, provide parsing and conversion methods, and support autoboxing. Full answer: Java end-sem 2025, Q2.1.
Q3.2 Explain any five StringBuffer methods with examples.
append, insert, replace, delete, reverse (plus length/capacity). Program with output: Java end-sem 2025, Q4.1.
Q3.3 Explain any 5 array methods with example.
Arrays.sort, toString, binarySearch, fill, copyOf, equals, and the length field. Program with output: Java end-sem 2025, Q3.2.
Q3.4 Explain the Scanner class in Java.
java.util.Scanner reads and parses input (keyboard, file or string) into tokens separated by whitespace.
- Create:
Scanner sc = new Scanner(System.in);(ornew Scanner(new File("data.txt"))). - Read methods:
nextInt(),nextDouble(),nextLong(),nextBoolean(),next()(one word),nextLine()(whole line). - Check methods:
hasNextInt(),hasNext(),hasNextLine()to validate before reading. - Close with
sc.close(). - Pitfall: after
nextInt(), the newline stays in the buffer, so a followingnextLine()returns an empty string; call an extranextLine()first. - Wrong type input throws
InputMismatchException.
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner("21 Asha 8.75\nComputer Engineering");
int age = sc.nextInt();
String name = sc.next();
double cpi = sc.nextDouble();
sc.nextLine(); // consume the rest of line 1
String branch = sc.nextLine();
System.out.println(name + ", " + age + ", CPI " + cpi + ", " + branch);
sc.close();
}
}
Output: Asha, 21, CPI 8.75, Computer Engineering. (In an exam, use new Scanner(System.in); a string source is used here so the run is reproducible.)
Q4. Attempt any one out of two [5]
Q4.1 (1) BankAccount with deposit(), withdraw() and display(). (2) Three integers in decreasing order.
(1)
class BankAccount {
private String holder;
private double balance;
BankAccount(String holder, double opening) { this.holder = holder; this.balance = opening; }
void deposit(double amt) {
if (amt > 0) balance += amt;
else System.out.println("Invalid deposit");
}
void withdraw(double amt) {
if (amt > balance) System.out.println("Insufficient balance for " + amt);
else balance -= amt;
}
void display() { System.out.println(holder + " balance: " + balance); }
}
public class BankDemo {
public static void main(String[] args) {
BankAccount acc = new BankAccount("Riya", 1000);
acc.display();
acc.deposit(500); acc.display();
acc.withdraw(300); acc.display();
acc.withdraw(5000);
acc.display();
}
}
Output:
Riya balance: 1000.0
Riya balance: 1500.0
Riya balance: 1200.0
Insufficient balance for 5000.0
Riya balance: 1200.0
(2) Program: Java end-sem 2025, Q4.6(b).
Q4.2 Find the output.
public class OutputCheck {
void display(int a) { System.out.println("int: " + a); }
void display(double a) { System.out.println("double: " + a); }
public static void main(String[] args) {
// Test
System.out.println(10 + 20 + "Java");
System.out.println("Java" + 10 + 20);
System.out.println("Java" + (10 + 20));
// Example
String s1 = "Hello";
String s2 = new String("Hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
// LoopTest
int count = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) continue;
count += i;
}
System.out.println(count);
// Test (overloading)
OutputCheck t = new OutputCheck();
t.display(5);
t.display(5.5f);
}
}
(The four programs from the paper, combined into one file to check them together.)
| Program | Output | Why |
|---|---|---|
| Test | 30Java · Java1020 · Java30 | + works left to right; once a String is involved it concatenates; brackets are evaluated first |
| Example | false · true | == compares references (pool object vs new heap object); equals() compares characters |
| LoopTest | 9 | even i are skipped: 1 + 3 + 5 |
| Test (overloading) | int: 5 · double: 5.5 | there is no float version, so 5.5f widens to double |