0% found this document useful (0 votes)
27 views4 pages

Java Exam Answers

The document provides exam answers related to Java programming, covering topics such as memory allocation for static and instance variables, encapsulation, inheritance types, and method overriding. It includes code examples demonstrating encapsulation with private variables and arrays, as well as techniques for finding duplicates in arrays. The content is structured in parts, with each part addressing specific Java concepts and principles.
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)
27 views4 pages

Java Exam Answers

The document provides exam answers related to Java programming, covering topics such as memory allocation for static and instance variables, encapsulation, inheritance types, and method overriding. It includes code examples demonstrating encapsulation with private variables and arrays, as well as techniques for finding duplicates in arrays. The content is structured in parts, with each part addressing specific Java concepts and principles.
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/ 4

Java Theory-Rich Exam Answers

Java Programming - Exam Answers

Part - A (2 × 5 = 10 marks)

1. How does the memory allocation of a static variable differ from an instance variable?

Static variables belong to the class, not to instances. Memory is allocated once in the method area, and the

variable is shared by all objects. Instance variables are object-specific and are stored in the heap, with each

object having its own copy.

2. In what scenarios would using a static variable be more efficient than a non-static variable?

Static variables are efficient when shared data is needed (e.g., a counter). They avoid redundant memory

allocation across instances.

3. Which access modifier is typically used to achieve encapsulation in Java?

The 'private' modifier is used to restrict access to class variables. Access is controlled through public

getter/setter methods.

4. What is encapsulation in Java?

Encapsulation is the wrapping of data and methods into a single unit and restricting direct access to data. It

ensures data integrity and security.

5. Explain how single inheritance differs from multilevel inheritance.

Single: One child inherits from one parent.

Multilevel: A class inherits from a derived class.

Example:

class A {} class B extends A {} (Single)

class A {} class B extends A {} class C extends B {} (Multilevel)

6. Differentiate between hierarchical and multilevel inheritance.

Hierarchical: Multiple classes inherit from a single parent.


Java Theory-Rich Exam Answers

Multilevel: Inheritance forms a chain.

Example (Hierarchical): class A; class B extends A; class C extends A;

Part - B (5 × 2 = 10 marks)

7. Encapsulation using private variables:

Declare variables private, provide public getter/setter methods.

Example:

class Person {

private String name;

public String getName() { return name; }

public void setName(String name) { this.name = name; }

8. Encapsulation with Student class:

class Student {

private int id;

private String name;

public void setId(int id) { this.id = id; }

public int getId() { return id; }

public void setName(String name) { this.name = name; }

public String getName() { return name; }

9. Method overriding demo:

class Shape {

double area() { return 0; }

class Rectangle extends Shape {

double area() { return 4 * 5; }

}
Java Theory-Rich Exam Answers

class Circle extends Shape {

double area() { return Math.PI * 3 * 3; }

Override allows specific implementation in subclasses.

Part - C (10 × 1 = 10 marks)

10. Arrays in Java:

- Fixed size, indexed, stored in heap.

- Declaration: int[] a;

- Initialization: a = new int[5];

- Iteration: loop through elements.

- Searching: compare in loop.

Example:

int[] nums = {10, 20, 30};

for (int i : nums) System.out.println(i);

Search: compare with target.

11. Anonymous Array & Duplicates:

Anonymous: used directly, no reference.

Example: display(new int[]{1, 2, 3});

Finding Duplicates:

1. Brute force

2. HashSet

3. Sort and compare

4. HashMap

Example with HashSet:

Set<Integer> seen = new HashSet<>();


Java Theory-Rich Exam Answers

for (int num : arr) if (!seen.add(num)) duplicates.add(num);

You might also like