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

Java Test PaperOOP

The document is a Java test paper covering Object-Oriented Programming, Collection Framework, and Stream API, with a total of 50 marks and a duration of 1 hour. It includes short answer questions and programming tasks that require demonstrating concepts such as inheritance, encapsulation, and the use of collections and streams. Each section has specific marks allocated, with programming questions requiring the implementation of Java code to solve given problems.

Uploaded by

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

Java Test PaperOOP

The document is a Java test paper covering Object-Oriented Programming, Collection Framework, and Stream API, with a total of 50 marks and a duration of 1 hour. It includes short answer questions and programming tasks that require demonstrating concepts such as inheritance, encapsulation, and the use of collections and streams. Each section has specific marks allocated, with programming questions requiring the implementation of Java code to solve given problems.

Uploaded by

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

Java Test Paper

Topics: OOP, Collection Framework, Stream API


Total Marks: 50
Time: 1 Hour

Section A: Object-Oriented Programming (20 Marks)

1. Short Answer Questions (2 Marks each)


(a) What is the difference between abstraction and encapsulation?
Ans. Abstraction hide the implementation only provide functionality while encapsulation bind variable and method
into single class and we cant access encapsulate method using (.) operator.

(b) Explain the concept of method overloading with an example.


Ans. Method overloading occurs when multiple method with same name in the same class but different
number of parameters.
* it is compile time polymorphism.

(c) What is an interface? How is it different from an abstract class?


Ans. Abstract hav both abstract and concrete method ,while interface have only unimplemented method.
* Abstract class have constructor but interface not have constructor.

(d) What is the purpose of the super keyword in Java?


Ans.The super keyword in java is used to refer to the parent class.it is used for access parent class
variable,call parent class method,call parent class constructor.
(e) Explain the use of constructors in Java.
Ans.A constructor in java is a special method used to initialize object.it is automatically called when object is
creat.
Features:
1.Same name as the class.
2.No return type.
3.Called automatically when an object is created.

2. Programming Questions
(a) (5 Marks) Write a Java program to demonstrate inheritance. Create a base class Animal with a method
makeSound(), and derive a class Dog that overrides this method.
Ans. package com.demo.javatest;

class Animal {
void makeSound(){
System.out.println("Animal Sound");
}
}
class Dog extends Animal{
@Override
void makeSound() {
System.out.println("Dog barks");
}

}
public class inheritenceTest {
public static void main(String[] args) {
Dog sound=new Dog();
sound.makeSound();
Animal s=new Animal();
s.makeSound();
}
}

(b) (5 Marks) Implement a Java class BankAccount with private fields accountNumber and balance.
Provide appropriate getter and setter methods and demonstrate encapsulation in the main method.
Ans. package com.demo.javatest;

class BankAccount {

private String accountNumber;


private double balance;

// Getter method for accountNumber


public String getAccountNumber() {
return accountNumber;
}

// Setter method for accountNumber


public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}

// Getter method for balance


public double getBalance() {
return balance;
}

// Setter method for balance


public void setBalance(double balance) {
if (balance >= 0) {
this.balance = balance;
} else {
System.out.println("Balance cannot be negative!");
}
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
} else {
System.out.println("Deposit amount must be positive!");
}
}

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
} else {
System.out.println("Insufficient balance or invalid amount!");
}
}
}

class Main {
public static void main(String[] args) {

BankAccount myAccount = new BankAccount();

myAccount.setAccountNumber("123456789");
myAccount.setBalance(500.0);

System.out.println("Account Number: " + myAccount.getAccountNumber());


System.out.println("Initial Balance: " + myAccount.getBalance());

myAccount.deposit(600.0);
System.out.println("Balance after deposit: " + myAccount.getBalance());

myAccount.withdraw(200.0);
System.out.println("Balance after withdrawal: " + myAccount.getBalance());

myAccount.withdraw(500.0);
}
}

Section B: Collection Framework (Basic) (15 Marks)

3. Short Answer Questions (2 Marks each)


(a) What is the difference between ArrayList and LinkedList?
Ans. 1 ArrayList backed by dynamicArray while LinkedList backed by dynamic list.
2.Fast random access,but slow insert/delete,while Faster insertion/deletion,but slower access.

(b) How does a HashSet ensure uniqueness?


(c) What is the difference between HashMap and TreeMap?
Ans. HashMap:
1. Does not maintain any specific order of its elements.
2. Internally uses a hash table to store the entries.
*TreeMap:
1.Maintains the keys in a sorted order.
2.Keys are sorted in ascending order by default (or based on the comparator if specified).

4. Programming Questions
(a) (5 Marks) Write a Java program to store five integer values in an ArrayList and print them using an
iterator.
Ans. package com.demo.javatest;

import java.util.ArrayList;

public class ArrayListTest {


public static void main(String[] args) {
ArrayList<Integer> numbers=new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
System.out.println("ArrayList :"+numbers);

for (int num:numbers){


System.out.println(num);
}

}
}

(b) (4 Marks) Create a HashMap to store student names as keys and their marks as values. Write a program to
print all students who scored above 80 marks.
Ans. package com.demo.javatest;

import java.util.HashMap;
import java.util.Map;

public class HashMapTest {


public static void main(String[] args) {
HashMap<String,Integer> student=new HashMap<>();
student.put("shubham",82);
student.put("Manoj",75);
student.put("Prdeep",68);
student.put("Bhanu",85);
student.put("student5",69);

for (Map.Entry<String,Integer> entry:student.entrySet()){


if (entry.getValue()>80){
System.out.println(entry.getKey()+"-"+entry.getValue());//passed student
}

}
}
}

Section C: Stream API (Basic) (15 Marks)

5. Short Answer Questions (2 Marks each)


(a) What is a stream in Java? How is it different from a collection?
Ans. Stream is apply on collection to perform operation like map(),filter(),sort()etc.

(b) Explain the purpose of the filter() method in Stream API.


Ans. The filter() method in the Java Stream API is used to filter elements from a stream based on
a specified condition.

(c) What is the difference between map() and forEach() in Java streams?

6. Programming Questions
(a) (5 Marks) Write a Java program to filter and print even numbers from a list using Java Stream API.
Ans. package com.demo.javatest;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(10, 25, 15, 16, 78, 49, 56, 44);
Predicate<Integer> isEven = num -> num % 2 == 0;

System.out.println("Even Numbers:" + numbers.stream().filter(isEven).collect(Collectors.toList()));

}
}

(b) (4 Marks) Given a list of names, use streams to convert all names to uppercase and print them.
Ans.

End of Test Paper

You might also like