Core Java Exam Paper 3

T.Y.B.B.A. (C.A.) CA 503: CORE JAVA – Exam Paper (2019 Pattern) (Semester-V)

Q1) Attempt any Eight:

a) What is Java? Why Java is a platform-neutral language?

Java is a high-level, object-oriented, class-based programming language developed by Sun Microsystems (now owned by Oracle). It is designed to have fewer implementation dependencies, making it portable across different platforms.

Platform-Neutral: Java is platform-neutral because of the concept of “Write Once, Run Anywhere” (WORA). Java programs are compiled into bytecode, which can run on any device that has a Java Virtual Machine (JVM) installed. The JVM interprets the bytecode and executes it, making the program independent of the underlying operating system.


b) What is access specifiers? List them.

Access Specifiers are keywords used to define the visibility or accessibility of classes, methods, and variables in Java.

  • Public: The member is accessible from any other class.
  • Private: The member is accessible only within the same class.
  • Protected: The member is accessible within the same package or by subclasses.
  • Default (no specifier): The member is accessible only within the same package.

c) Define Keyword-Static.

The static keyword in Java is used to indicate that a field, method, or block belongs to the class rather than instances of the class. It is shared by all instances of the class.

  • Static Variables: A variable that is shared by all instances of the class.
  • Static Methods: A method that can be called without creating an instance of the class.
  • Static Blocks: Used for initialization that can be run only once when the class is loaded.

Example:

class Counter {
    static int count = 0;

    static void increment() {
        count++;
    }

    public static void main(String[] args) {
        Counter.increment();
        System.out.println(Counter.count); // Output: 1
    }
}

d) Why we set environment variable in Java?

Environment variables are used in Java to configure various runtime parameters such as Java home, classpath, and path. They allow the JVM to know where to find certain resources or executables that are needed for the Java application to run.

For example:

  • JAVA_HOME: Specifies the location of the Java installation directory.
  • CLASSPATH: Defines where Java should look for classes.
  • PATH: Contains directories that the system searches for executable files.

e) Write advantages of Inheritance.

Advantages of Inheritance:

  1. Code Reusability: Allows the reuse of methods and fields from the parent class, reducing redundancy.
  2. Method Overriding: Child classes can provide specific implementations of methods inherited from the parent class.
  3. Extensibility: New classes can be created based on existing classes, making the system more flexible and scalable.
  4. Ease of Maintenance: Changes made to a superclass are automatically reflected in the subclasses.

f) Define class and object with one example.

  • Class: A blueprint or template for creating objects. It defines properties and behaviors.
  • Object: An instance of a class, containing specific values for the properties defined in the class.

Example:

class Car {
    String color;  // Property
    void start() { // Behavior
        System.out.println("Car is starting");
    }
}

public class TestCar {
    public static void main(String[] args) {
        Car myCar = new Car();  // Object creation
        myCar.color = "Red";
        myCar.start();  // Calling method
    }
}

g) What is Swing?

Swing is a part of Java’s GUI (Graphical User Interface) toolkit. It is used to create window-based applications with graphical elements such as buttons, labels, text fields, tables, etc. Swing is more advanced than AWT (Abstract Window Toolkit) and provides a rich set of GUI components.


h) When BufferedReader is used?

BufferedReader is used when reading data from files, input streams, or keyboard input in Java. It provides efficient reading of characters, arrays, and lines by buffering input data, which reduces the number of I/O operations and improves performance.

Example:

import java.io.*;

public class ReadFile {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
        String line = reader.readLine();
        System.out.println(line);
        reader.close();
    }
}

i) What is main difference between exception and error?

  • Exception: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. It can be handled using try-catch blocks.
    • Example: ArithmeticException, IOException
  • Error: An error is a serious problem that the application cannot recover from. It is usually caused by a failure in the JVM or environment.
    • Example: OutOfMemoryError, StackOverflowError

j) What is Panel?

A Panel is a container used in AWT (Abstract Window Toolkit) to group components together in a GUI. It does not have a title bar or a border, and it is used to organize GUI elements.

Example:

import java.awt.*;

public class TestPanel {
    public static void main(String[] args) {
        Frame f = new Frame("Test Panel");
        Panel p = new Panel();
        p.setBackground(Color.cyan);
        f.add(p);
        f.setSize(400, 400);
        f.setVisible(true);
    }
}

Q2) Attempt any four: (Full explanation)

a) What is Super Keyword? Explain the use of super keyword with suitable example.

The super keyword in Java is used to refer to the immediate parent class object. It is mainly used for:

  1. Accessing parent class methods.
  2. Accessing parent class constructors.
  3. Accessing parent class fields.

Example:

class Animal {
    void speak() {
        System.out.println("Animal speaks");
    }
}

class Dog extends Animal {
    void speak() {
        super.speak();  // Calls parent class method
        System.out.println("Dog barks");
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.speak();  // Output: Animal speaks, Dog barks
    }
}

b) Describe file handling in brief.

File handling in Java involves reading from and writing to files using classes from the java.io package. Common classes for file handling include:

  1. File: Represents the file or directory path.
  2. FileReader / FileWriter: Used for reading and writing character files.
  3. BufferedReader / BufferedWriter: Used for reading and writing files with efficient buffering.
  4. FileInputStream / FileOutputStream: Used for reading and writing byte files.

Example:

import java.io.*;

public class FileHandling {
    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter("output.txt");
        writer.write("Hello, world!");
        writer.close();

        FileReader reader = new FileReader("output.txt");
        int i;
        while ((i = reader.read()) != -1) {
            System.out.print((char) i);
        }
        reader.close();
    }
}

c) What is datatype? Explain types of datatypes used in Java.

A datatype in Java defines the type of data a variable can hold. Java has two types of datatypes:

  1. Primitive Data Types: These are the basic data types provided by Java. Examples include:
    • int: Integer values (e.g., 10, -5)
    • double: Decimal values (e.g., 5.5, 3.14)
    • char: Single characters (e.g., ‘a’, ‘1’)
    • boolean: true or false
  2. Reference Data Types: These are objects or arrays. They store the reference to the data.
    • Example: String, arrays, user-defined classes.

d) What is interface? Why they are used in Java?

An interface in Java is a reference type that can contain constants, method signatures, default methods, static methods, and nested types. Interfaces cannot have constructors and cannot contain instance variables.

Why interfaces are used:

  • They allow classes to implement multiple interfaces, enabling multiple inheritance.
  • They provide a way to achieve abstraction, where the implementation of methods is left to the class that implements the interface.

Example:

interface Animal {
    void sound(); // Abstract method
}

class Dog implements Animal {
    public void sound() {
        System.out.println("Bark");
    }
}

public class TestInterface {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();  // Output: Bark
    }
}

e) Why the main() method in public static? Can we overload it? Can we run java class without main() method?

  • Public: The main() method must be public so that it is accessible by the JVM when the program starts.
  • Static: The main() method is static so that it can be called without creating an instance of the class.

Overloading the main() method: Yes, the main() method can be overloaded by defining it with different parameter lists, but the JVM will always call the standard main(String[] args) method.

Running a class without a main() method: Normally, a Java class is executed through the main() method. However, certain Java programs, like Applets, do not require a main() method as the execution starts automatically through specific methods such as init() or start().


Q3) Attempt any four:

a) Write a Java program which accepts student details (Sid, Sname, Saddr) from user and display it on next frame. (Use AWT)

import java.awt.*;
import java.awt.event.*;

public class StudentDetails extends Frame {
    TextField sidField, snameField, saddrField;
    Button submitButton;

    public StudentDetails() {
        setLayout(new FlowLayout());

        add(new Label("Student ID:"));
        sidField = new TextField(20);
        add(sidField);

        add(new Label("Student Name:"));
        snameField = new TextField(20);
        add(snameField);

        add(new Label("Student Address:"));
        saddrField = new TextField(20);
        add(saddrField);

        submitButton = new Button("Submit");
        add(submitButton);

        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String sid = sidField.getText();
                String sname = snameField.getText();
                String saddr = saddrField.getText();

            Frame newFrame = new Frame("Student Details");
            newFrame.setLayout(new FlowLayout());
            newFrame.add(new Label("Student ID: " + sid));
            newFrame.add(new Label("Student Name: " + sname));
            newFrame.add(new Label("Student Address: " + saddr));
            newFrame.setSize(300, 200);
            newFrame.setVisible(true);
        }
    });

    setSize(300, 200);
    setVisible(true);
}

public static void main(String[] args) {
    new StudentDetails();
}
}
Scroll to Top