0% found this document useful (0 votes)
8 views9 pages

Java Programming Concepts and Examples

The document contains a series of Java programming questions and answers covering topics such as the use of javac, wrapper classes, constructors, command line arguments, GUI components, exception handling, and user-defined exceptions. It includes code examples for creating a triangle area calculation, file copying with character case changes, and perfect number identification. Additionally, it discusses the final keyword's usage and class inheritance with examples.

Uploaded by

ralebhatdaksh06
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)
8 views9 pages

Java Programming Concepts and Examples

The document contains a series of Java programming questions and answers covering topics such as the use of javac, wrapper classes, constructors, command line arguments, GUI components, exception handling, and user-defined exceptions. It includes code examples for creating a triangle area calculation, file copying with character case changes, and perfect number identification. Additionally, it discusses the final keyword's usage and class inheritance with examples.

Uploaded by

ralebhatdaksh06
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

Q1)

a) What is use of javac?


It is a Java compiler used to convert Java source code (.java) into bytecode
(.class).

b) Give the name of any two wrapper classes.


Integer, Double

c) What is use of implements keyword?


It is used by a class to implement an interface.

d) List types of constructor.


Default constructor, Parameterized constructor.

e) What is use of Array?


Array is used to store multiple values of the same data type in a single variable.

f) Give the name of any two listeners.


ActionListener, KeyListener

g) What is exception?
An exception is an unwanted event that disrupts the normal flow of a program.

h) Give the syntax of endsWith() method.


boolean endsWith(String suffix)

i) What is package?
A package is a collection of related classes and interfaces.

j) What is use of new operator?


It is used to create objects in Java.
Q2)

a) When constructor of class will be called? Comment.


A constructor is called automatically when an object of the class is created
using the new keyword.
It is used to initialize the object.

b) What is command line argument? Where they are stored in a program.


Command line arguments are values passed to the program at the time of
execution.
They are stored in the String args[] array of the main(String args[]) method.

c) What is Frame? Give its any two methods.


A Frame is a top-level window in Java AWT used to create GUI applications.
Two methods are:

 setSize(int w, int h) → sets the size of frame.


 setTitle(String title) → sets the title of frame.

d) Differentiate between method overloading and method overriding.

 Overloading: Same method name, different parameters, occurs within


the same class (compile-time polymorphism).
 Overriding: Subclass redefines parent’s method with the same signature
(runtime polymorphism).

e) Write any two access specifiers.

 public: Members accessible from anywhere in the program.


 private: Members accessible only within the same class.
Q3)

a) Define an interface shape with abstract method area( ). Inherit interface


shape into the class [Link] a Java Program to calculate area of Triangle.

interface Shape {

void area();

class Triangle implements Shape {

double base, height;

Triangle(double b, double h) {

base = b;

height = h;

public void area() {

double a = 0.5 * base * height;

[Link]("Area of Triangle = " + a);

public static void main(String[] args) {

Triangle t = new Triangle(10, 5); // Example values

[Link]();

}
c) Write a Java Program to copy the contents form one file into another file.
While copying, change the case of cell the alphabets & replace all the digital by
'*'.

import [Link].*;

class FileCopy {

public static void main(String[] args) {


try {
int ch;
while ((ch = [Link]()) != -1) {
char c = (char) ch;
if ([Link](c)) {
c = [Link](c);
} else if ([Link](c)) {
c = [Link](c);
}
if ([Link](c)) {
c = '*';
}
[Link](c);
}
[Link]();
[Link]();
[Link]("File copied successfully with modifications!");
} catch (Exception e) {
[Link]("Error: " + e);
} } }
Q4)
a) Differentiate between AWT & Swing.

AWT Swing
Heavyweight components Lightweight components (do not depend
(depend on OS). on OS).
Rich set of GUI components (tables, trees,
Limited set of GUI components.
sliders, etc.).
Slower performance compared to
Faster and more flexible.
Swing.
Package: [Link].* Package: [Link].*
Example: Frame, Button. Example: JFrame, JButton.
b) Define user define exception zeronumber Exc. Write a Java program to
accept a number from user. If it is zero then throw user define exception
"Number is zero" otherwise calculate the sum of first & last digit of
given number. (use Static Keyword).
import [Link];
class ZeroNumberExc extends Exception {
ZeroNumberExc(String msg) {
super(msg); } }
class SumDigits {
static int sumFirstLast(int num) {
int last = num % 10;
int first = num;
while (first >= 10) {
first = first / 10; }
return first + last; }
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter a number: ");
int n = [Link]();
if (n == 0)
throw new ZeroNumberExc("Number is Zero");
int sum = sumFirstLast(n);
[Link]("Sum of First and Last digit = " + sum);
} catch (ZeroNumberExc e) {
[Link]([Link]());
}
} }
c) Write a Java program to accept n number from user & store only perfect
numbers into array & display that array.
import [Link];
class PerfectArray {
static boolean isPerfect(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) sum += i; }
return sum == num; }
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter how many numbers: ");
int n = [Link]();
int arr[] = new int[n];
int perfect[] = new int[n];
int k = 0;
[Link]("Enter numbers:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
if (isPerfect(arr[i])) {
perfect[k++] = arr[i]; }
}
[Link]("Perfect numbers are:");
for (int i = 0; i < k; i++) {
[Link](perfect[i] + " ");
}}}
a) Explain uses of final keyword with example.

The final keyword in Java is used to restrict modification. Its uses are:

1. Final Variable → Once assigned, value cannot be changed.


o Example: final int x = 10;
2. Final Method → Cannot be overridden by subclass.
o Example:
o class A { final void show(){} }
o class B extends A { /* cannot override show() */ }
3. Final Class → Cannot be inherited.
o Example: final class Test { }
b) Define a class Emp with a member Eid and display() method, inherit EmP class into the
Emp Name class, Emp Name class having a member Ename & display ( ) method. Write a
Java program to accept details of employee [Eid, Ename] & display it. (Use super keyword).
import [Link];
class Emp {
int Eid;
void display() {
[Link]("Employee ID: " + Eid);
}
}
class EmpName extends Emp {
String Ename;
void display() {
[Link]();
[Link]("Employee Name: " + Ename);
}
}
class TestEmp {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
EmpName e = new EmpName();
[Link]("Enter Employee ID: ");
[Link] = [Link]();
[Link]();
[Link]("Enter Employee Name: ");
[Link] = [Link]();
[Link]();
}
}

You might also like