VNSGU BCA Sem 4: Java Programming (403) Practical Solutions - April 2025 Set EΒΆ
Paper Details
- Subject: Java Programming Language
- Subject Code: 403
- Set: E
- Semester: 4
- Month/Year: April 2025
- Max Marks: 25
- Time Recommendation: 45 Minutes
- Paper: View Paper | Download PDF
Questions & SolutionsΒΆ
All questions are compulsoryΒΆ
Q1: OOP - Student ClassΒΆ
Max Marks: 20
Create a Student class with the following specifications:
- Attributes: name, rollNo, marks
- Method: display() to show student details
- Create objects and demonstrate the class
1. Understanding Class StructureΒΆ
Learn the components of a Java class.
Hint
- Class: Blueprint for creating objects
- Attributes (Fields): Variables that store object state
- Methods: Functions that define object behavior
- Constructor: Special method for object initialization
classDiagram
class Student {
-String name
-int rollNo
-double marks
+Student(String, int, double)
+display()
}
2. Create the Student ClassΒΆ
Define the class with attributes and constructor.
Hint
Use private access modifier for encapsulation. Create a constructor to initialize values.
View Solution - Class Definition
// Student class definition
class Student {
// Private attributes (encapsulation)
private String name;
private int rollNo;
private double marks;
// Constructor to initialize student data
public Student(String name, int rollNo, double marks) {
this.name = name;
this.rollNo = rollNo;
this.marks = marks;
}
// Method to display student details
public void display() {
System.out.println("Student Details:");
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Marks: " + marks);
System.out.println("-------------------");
}
}
Step-by-Step Explanation:
- Initialization: Declare private fields for name, rollNo, marks
- Logic Flow:
- Constructor takes parameters and assigns to fields using
this thisdistinguishes instance variables from parameters
- Constructor takes parameters and assigns to fields using
- Completion: Class is ready to create student objects
3. Create Main Class with ObjectsΒΆ
Demonstrate the Student class by creating objects.
Hint
Create multiple student objects with different data to show the class works.
View Complete Solution & Output
// Main class to demonstrate Student class
public class StudentDemo {
public static void main(String[] args) {
// Create first student object
Student student1 = new Student("Rahul Sharma", 101, 85.5);
// Create second student object
Student student2 = new Student("Priya Patel", 102, 92.0);
// Create third student object
Student student3 = new Student("Amit Kumar", 103, 78.5);
// Display all student details
System.out.println("=== Student Information System ===\n");
student1.display();
student2.display();
student3.display();
// Additional: Calculate and display average marks
double average = (student1.marks + student2.marks + student3.marks) / 3;
System.out.println("Average Marks: " + average);
}
}
// Student class
class Student {
String name;
int rollNo;
double marks;
// Constructor
Student(String name, int rollNo, double marks) {
this.name = name;
this.rollNo = rollNo;
this.marks = marks;
}
// Display method
void display() {
System.out.println("Student Details:");
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Marks: " + marks);
System.out.println("-------------------");
}
}
Output:
=== Student Information System ===
Student Details:
Name: Rahul Sharma
Roll No: 101
Marks: 85.5
-------------------
Student Details:
Name: Priya Patel
Roll No: 102
Marks: 92.0
-------------------
Student Details:
Name: Amit Kumar
Roll No: 103
Marks: 78.5
-------------------
Step-by-Step Explanation:
- Initialization: Create Student class with fields, constructor, and display method
- Logic Flow:
- In main, create three Student objects with different data
- Call display() method on each object
- Completion: Program displays all student details
4. Enhanced Version with User InputΒΆ
Allow dynamic student creation with Scanner.
Enhanced Solution with User Input
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Create array to store students
Student[] students = new Student[3];
// Input student data
for (int i = 0; i < 3; i++) {
System.out.println("\nEnter details for Student " + (i + 1) + ":");
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Roll No: ");
int rollNo = sc.nextInt();
System.out.print("Enter Marks: ");
double marks = sc.nextDouble();
sc.nextLine(); // Consume newline
// Create student object
students[i] = new Student(name, rollNo, marks);
}
// Display all students
System.out.println("\n=== Student Information ===");
for (Student s : students) {
s.display();
}
sc.close();
}
}
class Student {
private String name;
private int rollNo;
private double marks;
// Constructor
public Student(String name, int rollNo, double marks) {
this.name = name;
this.rollNo = rollNo;
this.marks = marks;
}
// Display method
public void display() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Marks: " + marks);
System.out.println("-------------------");
}
// Getter methods (encapsulation)
public String getName() { return name; }
public int getRollNo() { return rollNo; }
public double getMarks() { return marks; }
}
Step-by-Step Explanation:
- Initialization: Create Student array and Scanner for input
- Logic Flow:
- Loop to get 3 student details from user
- Create Student objects and store in array
- Loop to display all student information
- Completion: Interactive student management system
Concept Deep Dive: OOP Principles
Four Pillars of OOP:
- Encapsulation: Bundling data and methods, hiding internal state (private fields)
- Inheritance: Creating new classes from existing ones (extends)
- Polymorphism: Same interface, different implementations (method overriding)
- Abstraction: Hiding complexity, showing essential features (abstract classes)
Constructor vs Method: - Constructor: Same name as class, no return type, called during object creation - Method: Any name, has return type, called on existing objects
this Keyword: - Refers to current object - Distinguishes instance variables from parameters - Can call other constructors using this()
Q2: Viva PreparationΒΆ
Max Marks: 5
Potential Viva Questions
- Q: What is a class in Java?
-
A: A class is a blueprint or template for creating objects. It defines attributes (fields) and behaviors (methods) that objects will have.
-
Q: What is the difference between a class and an object?
-
A: A class is a template/blueprint, while an object is an instance created from that class. One class can create many objects.
-
Q: What is a constructor?
-
A: A special method with the same name as the class, used to initialize objects when they are created. It has no return type.
-
Q: What does the 'this' keyword do?
-
A: It refers to the current object instance. Used to distinguish instance variables from parameters with same names.
-
Q: What is encapsulation?
-
A: Wrapping data and methods together in a class and hiding internal details. Achieved using private fields and public getters/setters.
-
Q: Why use private fields instead of public?
-
A: To control access to data, validate changes, and maintain data integrity. This is the principle of encapsulation.
-
Q: Can we have multiple constructors in a class?
- A: Yes, it's called constructor overloading. Multiple constructors with different parameter lists.
Common Pitfalls
- Forgetting 'this': When parameters have same name as fields, use this.fieldName
- Null pointer: Don't call methods on null references
- Access modifiers: Private fields can't be accessed directly from outside class
- Missing constructor: If no constructor defined, Java provides default no-arg constructor
- Scanner issues: Remember to consume newline after nextInt() or nextDouble()
Quick NavigationΒΆ
Related SolutionsΒΆ
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Current Page |
| Set F | Solutions |
Last Updated: April 2026