FYUG Even Semester Exam, 2025
COMPUTER SCIENCE: CSCDSC-251
(Object-oriented Programming with Java)

Subject: Computer Science

Paper Code: CSCDSC-251

Semester: 4th Semester (FYUG)

Year: 2025


UNIT-I

Question 1 (a) [2 Marks]

Why is Java called a platform independent language?

Java is platform independent because of the Bytecode. Unlike other languages that compile code into machine-specific instructions, Java compiles source code into Bytecode (.class files). This Bytecode can run on any system that has a Java Virtual Machine (JVM) installed, following the "Write Once, Run Anywhere" (WORA) principle.

Question 1 (b) [2 Marks]

What are the various data types used in Java programming?

Java data types are divided into two categories:

Question 1 (c) [2 Marks]

Write a Java program to display 'Hello World'.

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }

Question 2 (a) [10 Marks]

(i) Discuss the major features of Java.

(ii) Write a Java program to calculate the factorial of a given number.

import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); long factorial = 1; for(int i = 1; i <= num; ++i) { factorial *= i; } System.out.println("Factorial: " + factorial); } }

Question 2 (b) [10 Marks]

(i) Write the differences between C++ and Java.

Feature C++ Java
Platform Platform Dependent Platform Independent
Pointers Supports Pointers No Pointers
Inheritance Multiple Inheritance Single Inheritance (Classes)

(ii) Write a Java program to generate Fibonacci series.

public class Fibonacci { public static void main(String[] args) { int n = 10, t1 = 0, t2 = 1; for (int i = 1; i <= n; ++i) { System.out.print(t1 + " "); int sum = t1 + t2; t1 = t2; t2 = sum; } } }

UNIT-II

Question 3 (a) [2 Marks]

How can a multi-dimensional array be used in Java? Give example.

A multi-dimensional array is an array of arrays. It is commonly used to represent matrices.

int[][] matrix = { {1, 2}, {3, 4} };

Question 3 (b) [2 Marks]

Write the importance of Scanner class in Java.

The Scanner class (java.util.Scanner) is used to get user input. It can read various data types like int, double, and strings from the console or files.

Question 3 (c) [2 Marks]

How does String class differ from String buffer?

Question 4 (a) [10 Marks]

(i) Explain method overloading with the help of an example.

Method overloading allows a class to have more than one method with the same name, provided their parameter lists are different.

class MathOp { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }

(ii) What is garbage collection and why is it important?

Garbage collection is the process by which Java programs perform automatic memory management. It deletes unused objects to free up heap memory, preventing memory leaks.

UNIT-III

Question 5 (a) [2 Marks]

What is wrapper class?

A wrapper class is a class whose object wraps or contains primitive data types (e.g., Integer for int, Double for double).

Question 5 (b) [2 Marks]

Define method overriding.

Method overriding occurs when a subclass provides a specific implementation for a method already defined in its parent class.

Question 6 (a) [10 Marks]

(i) Why does Java not support multiple inheritance? Write code for multilevel inheritance.

Java avoids multiple inheritance to prevent ambiguity (the Diamond Problem). If two parent classes have the same method, the compiler wouldn't know which one to inherit.

class A { void msg(){System.out.println("A");} } class B extends A { void msg(){System.out.println("B");} } class C extends B { } // Multilevel

(ii) Discuss dynamic method dispatch.

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime rather than compile-time.

UNIT-IV

Question 7 (a) [2 Marks]

Define exception. How does exception differ from error?

  • Exception: Events that can be caught and handled by the program (e.g., NullPointerException).
  • Error: Irrecoverable conditions that the program usually cannot handle (e.g., OutOfMemoryError).

Question 8 (a) [10 Marks]

(i) What is multithreading? Write benefits.

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU.

  • Better resource utilization.
  • Responsive UI.
  • Faster execution of complex tasks.

(ii) Explain steps to create your own exception.

  1. Create a class that extends Exception.
  2. Define a constructor.
  3. Use throw to trigger it.

UNIT-V

Question 10 (b) [10 Marks]

(i) Differences between AWT and Swing.

AWT Swing
Heavyweight Lightweight
Platform Dependent Platform Independent

(ii) Design a simple calculator using Java Swing.

import javax.swing.*; public class Calc { public static void main(String[] args) { JFrame f = new JFrame("Calculator"); JButton b = new JButton("Add"); b.setBounds(50,100,80,30); f.add(b); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } }