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

Java Programming - Model Qus Paper - 2 by Silas

Uploaded by

silasmacx
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)
13 views

Java Programming - Model Qus Paper - 2 by Silas

Uploaded by

silasmacx
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/ 5

Java Programming Question Paper -

2025 (Set 2)

Madurai Kamaraj University - B.Sc Computer


Science - 4th Semester

Part A - One Mark Questions (10 x 1 = 10 Marks)

1. What is JVM?

Java Virtual Machine

2. Which keyword is used to inherit a class?

extends

3. What is the size of int in Java?

4 bytes

4. Which method is used to start a thread?

start()

5. What is the default value of a boolean variable in Java?

false

6. Which package contains Scanner class?

java.util
7. What does JDK stand for?

Java Development Kit

8. What is the return type of main method?

void

9. Which operator is used for comparison?

==

10. Which access specifier makes members accessible everywhere?

public

Part B - 5 Mark Questions (5 x 5 = 25 Marks)

1. Explain OOP concepts in Java.

Java is an object-oriented programming language that follows several core


principles of OOP:

1. Encapsulation: Binding data and code together into a single unit known as a
class, restricting access using access modifiers.
2. Inheritance: Acquiring properties and behaviors from another class using
the "extends" keyword, promoting code reuse.
3. Polymorphism: Allowing the same method to have different
implementations (method overloading and overriding).
4. Abstraction: Hiding internal implementation and showing only the
functionality, implemented through abstract classes and interfaces.
5. Class and Object: Everything in Java is associated with classes and objects; a
class is a blueprint, and an object is an instance.
These features allow for modular, scalable, and maintainable code.

2. Write a Java program to check whether a number is prime or not.


A prime number is one that has only two factors: 1 and itself. Here’s how we
check that:

```java
import java.util.*; class PrimeCheck { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int
n = sc.nextInt(); boolean prime = true; for (int i = 2; i <= Math.sqrt(n); i++) { if (n
% i == 0) { prime = false; break; } } if (prime && n > 1) System.out.println(n + "
is a Prime Number"); else System.out.println(n + " is not a Prime Number"); } }
```

3. Describe different types of constructors in Java.

Java has three types of constructors:


1. Default Constructor: Takes no parameters and is provided automatically by
Java if no constructor is defined.
2. Parameterized Constructor: Takes arguments to initialize an object with
specific values.
3. Copy Constructor: Creates an object by copying values from another object
of the same class. Although Java does not have built-in support, it can be
manually defined.
Constructors are used for object initialization and are invoked automatically at
object creation.

4. Explain exception handling in Java.

Exception handling is used to handle runtime errors and maintain normal


flow of the application. Java provides five keywords for this:
try: Contains code that may throw an exception.
catch: Catches and handles the exception.
finally: Block that executes after try/catch whether an exception occurs or not.
throw: Used to explicitly throw an exception.
throws: Declares exceptions that a method might throw.
Example: ```java try { int a = 10 / 0; } catch (ArithmeticException e) {
System.out.println("Division by zero error"); } finally { System.out.println("End
of block"); } ```
5. Write about data types in Java.

Java supports two types of data types:


1. Primitive Types: byte, short, int, long, float, double, boolean, char. These are
basic types used to hold simple values.
2. Non-Primitive Types: Includes String, Arrays, and user-defined classes.
They store references to memory locations.
Choosing the correct data type helps optimize memory usage and
performance.

Part C - 8 Mark Questions (5 x 8 = 40 Marks)

1. Explain the concept of multithreading in Java with example.

Multithreading allows concurrent execution of two or more parts of a


program. It improves CPU utilization and performance. Java supports
multithreading by extending the Thread class or implementing Runnable
interface. Example: ```java class MyThread extends Thread { public void run()
{ for (int i = 0; i < 5; i++) System.out.println(Thread.currentThread().getId() + "
Value: " + i); } } public class Demo { public static void main(String[] args) {
MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); t1.start();
t2.start(); } } ``` Threads execute independently and may run simultaneously
based on the CPU core availability.

2. Describe the life cycle of an applet.

An applet goes through five states: 1. init(): Initializes the applet. 2. start():
Starts or resumes execution. 3. paint(): Draws the content. 4. stop(): Stops the
execution temporarily. 5. destroy(): Cleanup before unloading the applet.
Applets run in browsers using the JVM and do not have a main() method. Their
life cycle is controlled by the browser or applet viewer.

3. Explain Java I/O streams in detail.

Java I/O uses streams to read and write data. There are two types:
Byte Streams: Handles binary data using InputStream and OutputStream
classes.
Character Streams: Handles character data using Reader and Writer classes.
File I/O uses classes like FileInputStream, FileOutputStream, FileReader, and
FileWriter.
Buffered streams like BufferedReader improve efficiency by reducing direct
disk I/O.

4. Describe inheritance types supported in Java.

Java supports: Single Inheritance: One class inherits from another.


Multilevel Inheritance: A class inherits from another class which in turn
inherits from another.
Hierarchical Inheritance: Multiple classes inherit from one superclass.
Hybrid Inheritance is supported using interfaces since Java doesn’t support
multiple inheritance through classes to avoid ambiguity.

5. Write a Java program to demonstrate polymorphism.

Polymorphism is the ability to perform a task in multiple ways. It is achieved


through method overloading and overriding. ```java class Animal { void
sound() { System.out.println("Animal makes a sound"); } } class Dog extends
Animal { void sound() { System.out.println("Dog barks"); } } public class
TestPoly { public static void main(String[] args) { Animal obj = new Dog();
obj.sound(); // Output: Dog barks } } ``` This demonstrates runtime
polymorphism through method overriding.

Download as PDF

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy