Java University Paper 2
Java University Paper 2
*******Paper-2*******
c) Define Keyword-Static.
Ans:-
The static keyword in Java indicates that a variable or method belongs to the class rather than
instances of the class. This means that static members can be accessed without creating an
instance of the class. Static variables are shared among all instances, while static methods can
only directly access other static members.
g) What is Swing?
Ans:-
Swing is a part of Java's standard library that provides a set of lightweight GUI components for
building graphical user interfaces (GUIs). Unlike AWT components, Swing components are
platform-independent and offer more flexibility and features, such as pluggable look-and-feel,
advanced controls (tables, trees), and support for drag-and-drop operations.
j) What is Panel?
Ans:-
A Panel in Java is a container class used to group related components together within a GUI
application. It can hold multiple UI elements like buttons, text fields, and labels, allowing for
better organization and layout management within a window or frame.
Output:
I am an animal.
I am a dog.
2. Reference Data Types / Non-Primitive Data Types: These include any object or array type
that refers to an instance in memory.
e) Why is the main() method public static? Can we overload it? Can we run a Java class without
the main() method?
Ans:-
The main() method must be declared as public so that it can be accessed by the JVM from
outside the class. It must also be static so that it can be invoked without creating an instance of
the class. Yes, we can overload the main() method by providing different parameter lists;
however, only the standard signature (public static void main(String[] args)) will be called by the
JVM at runtime. A Java class cannot run without a main() method unless it is invoked from
another class that contains a main() method.
a) Write a java program which accepts student details (Sid, Sname, Saddr) from user and
display it on next frame. (Use AWT).
Ans:-
import java.awt.*;
import java.awt.event.*;
public StudentDetails() {
// Create UI components
Label sidLabel = new Label("Student ID:");
Label snameLabel = new Label("Student Name:");
Label saddrLabel = new Label("Student Address:");
// Layout setup
setLayout(new FlowLayout());
add(sidLabel);
add(sidField);
add(snameLabel);
add(snameField);
add(saddrLabel);
add(saddrField);
add(submitButton);
setSize(300, 200);
setVisible(true);
}
detailsFrame.add(detailsLabel);
detailsFrame.setSize(300, 100);
detailsFrame.setVisible(true);
}
b) Write a package MCA which has one class student. Accept student details through
parameterized constructor. Write display() method to display details. Create a main class which
will use package and calculate total marks and percentage.
Ans:-
// File: MCA/Student.java
package MCA;
public class Student {
private int sid;
private String name;
private String address;
private int marks1, marks2, marks3;
public Student(int sid, String name, String address, int marks1, int marks2, int marks3) {
this.sid = sid;
this.name = name;
this.address = address;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
}
// File: Main.java
import MCA.Student;
c) Write a Java program which accepts a string from user; if its length is less than five, then
throw user-defined exception “Invalid String”; otherwise display string in uppercase.
Ans:-
import java.util.Scanner;
try {
validateString(input);
System.out.println("Uppercase String: " + input.toUpperCase());
} catch (InvalidStringException e) {
System.out.println(e.getMessage());
}
}
e) What is recursion in Java? Write a Java Program to find factorial of a given number using
recursion.
Ans:-
Recursion in Java refers to a technique where a method calls itself to solve smaller instances of
the same problem until reaching a base condition.
import java.util.Scanner;
Example:
class MathOperations {
int add(int a, int b) { return a + b; } // Method with two int parameters
double add(double a, double b) { return a + b; } // Method with two double parameters
}
Example:
class Animal {
void sound() { System.out.println("Animal makes sound"); }
}
// Draw eyes
g.setColor(Color.BLACK);
g.fillOval(100, 100, 30, 30); // Left eye
g.fillOval(170, 100, 30, 30); // Right eye
// Draw mouth
g.drawArc(100, 120, 100, 60, 0, -180); // Mouth
}
}
d) Write a Java program to copy the data from one file into another file.
Ans:-
import java.io.*;
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine(); // Add newline after each line copied
}
System.out.println("File copied successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
e) Write a Java program to accept ‘n’ integers from the user store them in an ArrayList
Collection. Display the elements of ArrayList collection in reverse order.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
Example:
JTabbedPane tabbedPane1 = new JTabbedPane(); // Default constructor
JTabbedPane tabbedPane2 = new JTabbedPane(JTabbedPane.TOP); // Tabs at top
c) Abstract Class:
Ans:-
An abstract class in Java is a class that cannot be instantiated on its own and may contain
abstract methods (methods without implementation). Abstract classes are used as base classes
for other classes to extend them and provide specific implementations for abstract methods.
Example:
abstract class Animal {
abstract void sound(); // Abstract method
*******Paper-1*******
Q1) Attempt any Eight : [8 × 2 = 16]
j) Define polymorphism.
Ans:-
Polymorphism in Java refers to the ability of a single interface to represent different underlying
forms (data types). It allows methods to do different things based on the object it is acting upon,
typically achieved through method overriding (runtime polymorphism) and method overloading
(compile-time polymorphism).
b) What is the difference between constructor and method? Explain types of constructors.
Ans:-
A constructor is a special method invoked when an object is created, whereas a method is a
block of code that performs a specific task when called.
Types of constructors:
• Default Constructor: No arguments; initializes default values.
• Parameterized Constructor: Takes parameters to initialize object attributes.
🛑
Ans:-
Refer another answer for this🛑
d) Explain the concept of exception and exception handling.
Ans:-
An exception is an event that disrupts the normal flow of execution in a program. Exception
handling in Java is done using try-catch blocks:
• try block: Contains code that may throw an exception.
• catch block: Catches and handles exceptions thrown by the try block.
This mechanism allows developers to manage errors gracefully without crashing the program.
try {
int result = 10 / 0; // This will throw ArithmeticException
}
catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
}
b) Write a Java program to calculate area of circle, Triangle and Rectangle (Use Method
overloading).
Ans:-
class AreaCalculator {
double area(double radius) { // Area of Circle
return Math.PI * radius * radius;
}
c) Write a java program to accept n integers from the user and store them in an ArrayList
collection. Display elements in reverse order.
Ans:-
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
Collections.reverse(numbers);
d) Write a Java program to count number of digits, spaces and characters from a file.
Ans:-
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
e) Create an applet that displays x and y position of the cursor movement using mouse and
keyboard. (Use appropriate listener)
Ans:-
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
c) Write a java program to count number of Lines, words and characters from a given file.
Ans:-
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
d) Write a Java program to design an email registration form (Use Swing components).
Ans:-
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.add(panel);
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
e) Create a class Teacher (Tid, Tname, Designation, Salary, Subject). Write a java program to
accept 'n' teachers and display who teach Java subject (Use Array of object).
import java.util.Scanner;
class Teacher {
int tid;
String tname;
String designation;
double salary;
String subject;
Teacher(int tid, String tname, String designation, double salary, String subject) {
this.tid = tid;
this.tname = tname;
this.designation = designation;
this.salary = salary;
this.subject = subject;
}
}
a) Define object.
Ans:-
An object in Java is an instance of a class that contains state (attributes/fields) and behavior
(methods/functions). Objects are fundamental building blocks in object-oriented programming.
Example:
class Car {
// Properties (instance variables)
String model;
int year;
// Constructor
Car(String model, int year) {
this.model = model;
this.year = year;
}
// Method
void displayInfo() {
System.out.println("Car Model: " + model);
System.out.println("Car Year: " + year);
}
}
try {
// Code that may throw an exception
} catch (Exception e) {
// Handle exception
} finally {
// Cleanup code that always executes
}
c) What is package? Write down all the steps for package creation.
Ans:-
A package in Java is a namespace that organizes related classes and interfaces together. It
helps avoid naming conflicts and controls access.