0% found this document useful (0 votes)
3 views13 pages

java model paper 3

This document is a model paper on Object-Oriented Programming (OOP) using Java, covering key concepts such as platform independence, encapsulation, inheritance, polymorphism, and abstraction. It includes definitions, examples, and explanations of various Java features like constructors, arrays, exception handling, applets, and I/O streams. Additionally, it discusses advanced topics like method overloading, overriding, runtime polymorphism, and Java security management.

Uploaded by

usharushar238
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)
3 views13 pages

java model paper 3

This document is a model paper on Object-Oriented Programming (OOP) using Java, covering key concepts such as platform independence, encapsulation, inheritance, polymorphism, and abstraction. It includes definitions, examples, and explanations of various Java features like constructors, arrays, exception handling, applets, and I/O streams. Additionally, it discusses advanced topics like method overloading, overriding, runtime polymorphism, and Java security management.

Uploaded by

usharushar238
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/ 13

Oop’s using java

Model paper 3

2 marks

1. Why Java is called as Platform Independent?

Java is called platform independent because Java code runs on any operating system
using the Java Virtual Machine (JVM). You write code once and run it anywhere.

2. Define Encapsulation and Inheritance.

Encapsulation: Wrapping data and methods together in a class. It hides internal details.

Inheritance: One class can use the properties and methods of another class.

3. What is a constructor in Java?

A constructor is a special method used to create and initialize objects in Java. It has the
same name as the class.

4. What is an array? How it is created in Java?

An array is a group of similar data types. It is created using syntax like:

Int[] numbers = new int[5];

5. What are wrapper classes? Give an example.

Wrapper classes convert primitive types to objects.

Example: Integer a = new Integer(10);

6. What are final methods and final classes?

Final Method: Cannot be overridden by subclasses.

Final Class: Cannot be inherited.

7. How does an interface differ from an abstract class?

Interface has only method declarations (no body).

Abstract class can have both declared and defined methods.

8. What are Layout Managers? What is the use of Layout Managers in Java?

Layout Managers arrange GUI components in a window. They help control how buttons,
labels, etc., are placed.

9. What are local and remote applets?

Local Applet: Stored and run from your computer.


Remote Applet: Loaded from a server on the internet.

10. What is an Exception?

An exception is an error that happens while a program is running. Java uses try-catch to
handle it.

6 marks

11. Explain the four core concepts of Object-Oriented Programming (OOP) with
examples.

1. Encapsulation:

It means hiding the data and only showing what is necessary.

Example: A class has private variables and public methods to access them.

Class Person {

Private String name;

Public void setName(String n) { name = n; }

Public String getName() { return name; }

2. Inheritance:

It means one class can use the properties and methods of another class.

Example:

Class Animal {

Void sound() { System.out.println(“Animal sound”); }

Class Dog extends Animal {

Void bark() { System.out.println(“Dog barks”); }

3. Polymorphism:

It means performing the same action in different ways.

Example: Method Overriding

Class Animal {

Void sound() { System.out.println(“Animal sound”); }


}

Class Dog extends Animal {

Void sound() { System.out.println(“Dog barks”); }

4. Abstraction:

It means showing only important details and hiding the rest.

Example: Abstract class

Abstract class Shape {

Abstract void draw();

Class Circle extends Shape {

Void draw() { System.out.println(“Drawing Circle”); }

11. What are Java Tokens? Explain with an example.

Java Tokens are the smallest parts of a Java program that the compiler can understand.
There are 5 main types:

1. Keywords: Reserved words. Example: if, class, public

2. Identifiers: Names of variables, classes. Example: age, MyClass

3. Literals: Constant values. Example: 10, “Hello”

4. Operators: Symbols for operations. Example: +, -, *, /

5. Separators: Special symbols. Example: ;, {}, ()

Example:

Public class Test {

Public static void main(String[] args) {

Int a = 5 + 10;

In this example, tokens are: public, class, Test, (, ), int, a, =, 5, +, 10, ;


12. What is constructor overloading? How does it work? Explain with an example.

Constructor Overloading means having more than one constructor in a class with
different arguments.

It helps create objects in different ways.

Example:

Class Student {

Int id;

String name;

// Constructor 1

Student() {

Id = 0;

Name = “Unknown”;

// Constructor 2

Student(int i, String n) {

Id = i;

Name = n;

Void display() {

System.out.println(id + “ “ + name);

How it works:

Java chooses the constructor based on the number and type of arguments.

13. Explain the Standard I/O Streams in Java with examples.

Java uses I/O Streams to take input and give output.

Standard Input Stream:

Takes input from keyboard.


Uses System.in

Example using Scanner:

Import java.util.Scanner;

Public class Test {

Public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print(“Enter name: “);

String name = sc.nextLine();

System.out.println(“Hello “ + name);

Standard Output Stream:

Displays output on screen.

Uses System.out

System.out.println(“Welcome”);

Standard Error Stream:

Shows error messages.

Uses System.err

System.err.println(“Error occurred”);

14. Explain the difference between an applet and a standalone application.

Feature Applet Standalone Application

Runs in Web browser Java Virtual Machine (JVM)

Main() method Not used Starts with main() method

GUI support Uses AWT (Applet class) Uses AWT or Swing

Internet Support Can be downloaded from web Not from web

Security Limited access Full access to system resources

Applet Example:

Import java.applet.Applet;
Import java.awt.Graphics;

Public class HelloApplet extends Applet {

Public void paint(Graphics g) {

g.drawString(“Hello Applet”, 50, 50);

15. Explain sleep(), join() and isAlive() methods with an example.

All these methods are used in Thread handling in Java.

1. Sleep():

Pauses the thread for some time.

Thread.sleep(1000); // Sleeps for 1 second

2. Join():

Waits for one thread to finish before the next starts.

T1.join(); // Wait until t1 finishes

3. isAlive():

Checks if thread is still running.

If(t1.isAlive()) { ... }

Example:

Class MyThread extends Thread {

Public void run() {

For(int i=1; i<=5; i++) {

System.out.println(i);

Try { Thread.sleep(500); } catch(Exception e) {}

Public class Demo {

Public static void main(String[] args) throws InterruptedException {


MyThread t1 = new MyThread();

T1.start();

T1.join(); // Waits for t1 to finish

System.out.println(“Is thread alive? “ + t1.isAlive());

8 marks

17 (a). Describe the concept of type promotion in Java.

Type Promotion means converting smaller data types into larger ones automatically
during operations.

Java promotes byte, short, char to int before performing operations.

Example:

Byte a = 10;

Byte b = 20;

Int c = a + b; // promoted to int

System.out.println(c);

17 (b). What is for-each loop? How is it useful? Explain with an example.

For-each loop is used to iterate through arrays or collections easily.

It is useful when we do not need index values.

Syntax:

For(dataType var : array) {

// use var

Example:

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

For(int n : nums) {

System.out.println(n);

}
18 (a). Explain else-if ladder with an example.

Else-if ladder is used to test multiple conditions one after another.

Example:

Int marks = 75;

If(marks >= 90) {

System.out.println(“Grade A”);

} else if(marks >= 75) {

System.out.println(“Grade B”);

} else if(marks >= 60) {

System.out.println(“Grade C”);

} else {

System.out.println(“Fail”);

Q18 (b). Explain break and continue statements with an example.

Break: exits the loop early.

Continue: skips current loop step and goes to the next.

Example:

For(int i=1; i<=5; i++) {

If(i == 3) continue; // skip 3

System.out.println(i);

// Output: 1 2 4 5

For(int i=1; i<=5; i++) {

If(i == 3) break; // exit at 3

System.out.println(i);

// Output: 1 2
19 (a). What is Math class? Mention important mathematical functions.

Math class provides built-in methods to do mathematical operations.

Important Methods:

Math.abs(x) – Absolute value

Math.sqrt(x) – Square root

Math.pow(a, b) – a raised to power b

Math.max(a, b) – Returns maximum

Math.min(a, b) – Returns minimum

Math.random() – Generates random number between 0.0 and 1.0

Example:

System.out.println(Math.sqrt(16)); // 4.0

System.out.println(Math.pow(2, 3)); // 8.0

19 (b). Explain method overloading and overriding with examples.

Method Overloading: Same method name, different parameters in the same class.

Class Example {

Void show() { System.out.println(“No arg”); }

Void show(int a) { System.out.println(“Int arg”); }

Method Overriding: Subclass provides new version of a method from superclass.

Class Animal {

Void sound() { System.out.println(“Animal sound”); }

Class Dog extends Animal {

Void sound() { System.out.println(“Dog barks”); }

20 (a). How does method overriding support runtime polymorphism?

Runtime Polymorphism means deciding which method to run during execution time.

This happens through method overriding.


Example:

Class Animal {

Void sound() { System.out.println(“Animal sound”); }

Class Dog extends Animal {

Void sound() { System.out.println(“Dog barks”); }

Class Main {

Public static void main(String[] args) {

Animal a = new Dog(); // Parent reference, Child object

a.sound(); // Output: Dog barks

20 (b). Write a note on javax.swing package.

Javax.swing is used for creating Graphical User Interfaces (GUI) in Java.

Features:

Lightweight components

More powerful than AWT

Components: JFrame, JButton, JLabel, JTextField

Example:

Import javax.swing.*;

Public class MyWindow {

Public static void main(String[] args) {

JFrame f = new JFrame(“My App”);

JButton b = new JButton(“Click”);

b.setBounds(100, 100, 100, 40);

f.add(b);

f.setSize(300, 300);
f.setLayout(null);

f.setVisible(true);

21 (a). Explain primitive data handling in Java I/O.

Java uses DataInputStream and DataOutputStream to handle primitive data types like
int, char, double.

Example:

DataOutputStream dos = new DataOutputStream(new FileOutputStream(“data.txt”));

Dos.writeInt(10);

Dos.writeDouble(12.5);

To Read:

DataInputStream dis = new DataInputStream(new FileInputStream(“data.txt”));

Int i = dis.readInt();

Double d = dis.readDouble();

21 (b). Program to read and write character data using FileReader and FileWriter.

Program:

Import java.io.*;

Public class CharFile {

Public static void main(String[] args) throws IOException {

FileWriter fw = new FileWriter(“test.txt”);

Fw.write(“Hello File”);

Fw.close();

FileReader fr = new FileReader(“test.txt”);

Int ch;

While((ch = fr.read()) != -1) { System.out.print((char)ch);

Fr.close();
}

22 (a). Explain any 5 AWT controls with examples.

1. Label – Displays text

Label l = new Label(“Name:”);

2. TextField – Takes input

TextField tf = new TextField();

3. Button – Clickable button

Button b = new Button(“Submit”);

4. Checkbox – Select one or more options

Checkbox cb = new Checkbox(“Java”);

5. Choice – Dropdown menu

Choice c = new Choice();

c.add(“Java”);

c.add(“Python”);

22 (b). Explain the different types of errors in Java.

1. Syntax Errors: Mistakes in code format.

Example: Missing ;

2. Runtime Errors: Errors during program running.

Example: Divide by zero

3. Logical Errors: Program runs but wrong result.

Example: Wrong formula

4. Compilation Errors: Errors during compile time.

23 (a). What is Java Security Manager? How it works?

Java Security Manager controls what a Java program can and cannot do.

It provides security rules for programs (especially applets).

Example Rules:

Can’t read/write local files


Can’t access system properties

How it works: It checks permissions during runtime and blocks unsafe actions.

System.setSecurityManager(new SecurityManager());

23 (b). Write a note on Generic Classes and Generic Methods.

Generic Classes: Work with any data type.

Class Box<T> {

T value;

Void set(T val) { value = val; }

T get() { return value; }

Generic Methods: Methods that work with any data type.

Class Test {

Public <T> void print(T data) {

System.out.println(data);

You might also like