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

Java

Uploaded by

kutetriveni5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java

Uploaded by

kutetriveni5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

a) What is Super Keyword? Explain the use of super keyword with d) What is datatype?

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


suitable example.:- In object-oriented programming (OOP), the super keyword is is a classification of data based on its format, size, and set of values it can hold.
used to access the methods and properties of a parent or superclass from a child Datatypes determine the type of data that can be stored, processed, and
or subclass. It allows the child class to: manipulated in a programming language or database.
1. Primitive Datatypes: Basic datatypes that are built-in to the programming
language.
class Animal { public void sound() { 2. Derived Datatypes: Datatypes derived from primitive datatypes.
String name; super.sound(); // Calls Animal's 3. Reference Datatypes: Datatypes that store references to other data.
int age; sound method 4. Composite Datatypes: Datatypes composed of multiple values.
public Animal(String name, int System.out.println("The dog
age) { barks."); e) Explain method overloading Method overloading is a feature in Java that
this.name = name; }} allows multiple methods with the same name to be defined, as long as they have
this.age = age; } public class Main { different parameter lists.
public void sound() { public static void main(String[] Why Method Overloading?
System.out.println("The animal args) { 1. Improved Readability: Multiple methods with the same name make code
makes a sound."); Dog myDog = new Dog("Max", more readable.
}} 3, "Golden Retriever"); 2. Increased Flexibility: Methods with different parameters can handle various
class Dog extends Animal { scenarios.
String breed; System.out.println(myDog.name); 3. Reduced Errors: Compiler checks parameter types, reducing errors.
public Dog(String name, int age, myDog.sound();
String breed) { }} f) What is Layout Manager? Explain any one in detail. A Layout Manager is
super(name, age); // Calls Output: a crucial component in Java's Swing library, responsible for arranging and
Animal's constructor Max managing the size and position of graphical user interface (GUI) components
this.breed = breed; } The animal makes a sound. within a container. It ensures that components are properly aligned, resized, and
The dog barks. displayed. Types:- 1. FlowLayout 2. BorderLayout 3. GridLayout 4.
b) Describe file handling in brief. File handling refers to the process of storing, BoxLayout
retrieving, and manipulating data stored in files. It involves interactions between a
program and external files. g) How to create and access package in Java? Explain it with example. In
Key Concepts: Java, packages are used to organize related classes, interfaces, and other types.
1. File Types: Text (.txt), Binary (.bin), CSV (.csv), JSON (.json), etc. They provide a way to group related classes and avoid naming conflicts.
2. File Modes: Read (r), Write (w), Append (a), Read/Write (r+), etc. public class MyClass {
3. File Operations: public static void main(String[] args) {
- Create - Write - Read - Close System.out.println("Hello from MyClass!"); }}
- Open - Append - Append - Delete
File Handling Applications: h) Write a Java program to Fibonacci series.
1. Data storage and retrieval 2. Configuration files 3. Logging import java.util.Scanner; long[] fib = new long[n];
4. Text editors public class FibonacciSeries { fib[0] = 0; fib[1] = 1;
public static void main(String[] System.out.println("Fibonacci
c) What is interface? Why they are used in Java? In Java, an interface is an args) { Series:");
abstract class that defines a contract or a set of methods that must be Scanner scanner = new System.out.print(fib[0] + " " +
implemented by any class that implements it. An interface is essentially a Scanner(System.in); fib[1] + " ");
blueprint of methods without any implementation. System.out.println("Enter the for (int i = 2; i < n; i++) {
1. Abstraction 2. Polymorphism 3. Multiple Inheritance 4. number of terms:"); fib[i] = fib[i - 1] + fib[i - 2];
Loose Coupling int n = scanner.nextInt(); System.out.print(fib[i] + " ");
5. Testability 6. Extensibility 7. Contract scanner.close(); } } }

i) Explain anonymous class in detail. An anonymous class in Java is a class n) What is repaint method does? The repaint() method in Java is used to
that is defined without a name. It is a way to create a class that extends another request that the component be redrawn. It is a part of the Component class and is
class or implements an interface without declaring a separate named class. used to update the graphical user interface (GUI) of an application.
Syntax:- new [Access Modifier] [ClassName/InterfaceName]() { Functionality:
// Class body } 1. Requests the component to be redrawn.
interface Printable { Printable printable = new Printable() 2. Schedules a paint request to the event dispatch thread.
void print(); } { 3. Calls the paint() or paintComponent() method.
public class Main { public void print() {
public static void main(String[] o) What is exception? Expalin its keyword with example. An exception is
args) { System.out.println("Printing..."); } an event that occurs during the execution of a program, disrupting the normal
}; flow of instructions. Exceptions can be errors, faults, or unexpected events.
printable.print(); } } Types of Exceptions:-
j) Why Java is a platform neutral language? Java is considered a platform- 1. Checked Exceptions: These are exceptions that are checked at compile-time.
neutral language because of its unique architecture and design features, which 2. Unchecked Exceptions: These are exceptions that are not checked at
enable it to run on any device that has a Java Virtual Machine (JVM) installed. 1. compile-time.
Bytecode 2. Java Virtual Machine 3. Platform-independent APIs 3. Runtime Exceptions: These are exceptions that occur during runtime.
4. Memory management 5. No explicit pointers
p) What is collection? Explain Collection framework in details. A collection
k) Define multiple inheritance. Multiple inheritance is a feature of object- is a group of objects that are stored and manipulated together. The Java Collection
oriented programming (OOP) where a subclass can inherit properties and behavior Framework (JCF) provides a set of classes and interfaces for working with
from more than one superclass. collections. Key Components:-
Types:- 1. Multiple Interface Inheritance 2. Multilevel Inheritance 1. Interfaces: Define the contract for a collection.
public interface Printable { java 2. Classes: Implement the interfaces and provide concrete implementations.
void print(); } public class Animal { 3. Algorithms: Provide methods for common operations like sorting and
public interface Shareable { void sound() { searching.
void share(); } System.out.println("Animal import java.util.*; Map<String, Integer> map = new
public class Document implements makes a sound."); } } public class CollectionExample { HashMap<>();
Printable, Shareable { public class Mammal extends Animal public static void main(String[] map.put("Apple", 1);
public void print() { { args) { map.put("Banana", 2);
System.out.println("Printing void eat() { List<String> list = new map.put("Cherry", 3);
document..."); } System.out.println("Mammal ArrayList<>(); System.out.println("List: " + list);
public void share() { eats."); } } list.add("Apple"); System.out.println("Set: " +
System.out.println("Sharing public class Dog extends Mammal { list.add("Banana"); set);
document..."); void sound() { list.add("Cherry"); System.out.println("Map: " +
}} System.out.println("Dog map);
barks."); }} Set<String> set = new }}
l) How to handle events in applet? Explain with example. Event handling in HashSet<>();
applets involves responding to user interactions such as mouse clicks, keyboard set.add("Apple");
input, and window events. set.add("Banana");
Types:- 1. Mouse Events 2. Keyboard Events 3. Window Events 4. set.add("Cherry");
Action Events
Methods:- 1. init() 2. start() 3. stop() 4. destroy()

m) What are the different types of streams? 1. Byte Streams 2.


Character Streams 3. Object Streams 4. Piped Streams 5. Filter Streams
6. Print Streams
Q) Difference between Swing and AWT S) Difference between interface and abstract class
Swing AWT Interface Abstract class
1. The components of Java Swing 1. The components of Java AWT are 1. Interface supports multiple 1. Abstract class doesn't support
are light weighted. heavy weighted. inheritance. multiple inheritance.
2. Java Swing has more 2. Java AWT has comparatively less 2. Abstract class can have final, non-
functionality as compared to AWT. functionality as compared to Swing. 2. Interface has only static and final final, static and non-static variables.
3. The execution time of Swing is 3. The execution time of AWT is more variables 3. Abstract class can provide the
less than AWT. than Swing. implementation of interface.
4. The components of Java Swing 4. The components of Java AWT are 3. Interface can't provide the 4. The abstract keyword is used to
are platform independent. platform dependent. implementation of abstract class. declare abstract class.
5. MVC pattern is supported by 5. MVC pattern is not supported by 4. The interface keyword is used to 5. An abstract class can be extended
Swing. AWT. declare interface. using keyword "extends".
6. AWT provides comparatively less 5. An interface can be implemented 6. A Java abstract class can have
6. Swing provides more powerful powerful components. using keyword "implements". class members like private,
components. 7. AWT components require java.awt 6. Members of a Java interface are protected, etc.
7. Swing components requires package public by default.
javax.swing package T) Explain features of Java
1. Object-Oriented 2. Platform Independent 3. Simple 4. Secure 5.
R) Define new operator. The new operator in Java is used to create new objects, Multithreaded
arrays, and instances of classes. Syntax:- new ClassName(); new 6. Distributed 7. Robust 8. Dynamic 9. Extensive Libraries 10.
ClassName(arguments); new Type[] {values}; Open Source
Types:- 1. Object Creation 2. Array Creation 3.
Anonymous Classes U) Explain Java until package
4. Inner Classes 5. Multidimensional Arrays util package in Java is a built-in package that contains various utility classes and
interfaces. It provides basic functionality for commonly occurring use cases. It
contains Java's collections framework, date and time utilities, string-tokenizer,
event-model utilities, etc.
Types:- 1. Primitive Data Types 2. Reference Data Types

V) Write a Java program to check Armstrong number or not X) Write a Java program to calculate area of circle, rectangle
import java.util.Scanner; Armstrong number."); } else { Circle:- Rectangle:-
public class ArmstrongNumber { System.out.println(num + " is import java.util.Scanner; import java.util.Scanner;
public static void main(String[ ] not an Armstrong number."); } } public class CircleArea { public class RectangleArea {
args) { public static boolean isArmstrong(int public static void main(String[] public static void main(String[]
Scanner scanner = new num) { args) { args) {
Scanner(System.in); int temp = num; int sum = 0; Scanner scanner = new Scanner scanner = new
System.out.println("Enter a int digit; Scanner(System.in); Scanner(System.in);
number: "); while (temp != 0) { System.out.println("Enter the System.out.println("Enter the
int num = scanner.nextInt(); digit = temp % 10; radius of the circle:"); length of the rectangle:");
scanner.close(); sum += digit * digit * digit; double radius = double length =
if (isArmstrong(num) ) { temp /= 10; } scanner.nextDouble(); scanner.nextDouble();
System.out.println(num + " is return sum == num; } } scanner.close(); System.out.println("Enter the
an double area = Math.PI * radius * width of the rectangle:");
radius; double width =
System.out.println("The area of scanner.nextDouble();
the circle is: " + area); } } scanner.close();
W) Write Java program program to display all perfect number double area = length * width;
import java.util.Scanner; public static boolean isPerfect(int System.out.println("The area of
public class PerfectNumber { num) { the rectangle is: " + area); } }
public static void main(String[] int sum = 0;
args) { for (int i = 1; i < num; i++) { Y) Write a Java program to reverse order element in array
Scanner scanner = new if (num % i == 0) { import java.util.Scanner; public static void reverseArray(int[]
Scanner(System.in); sum += i; } } public class ArrayReverse { array)
System.out.println("Enter a return sum == num; } } public static void main(String[] { int left = 0;
limit: "); args) { int right = array.length - 1;
int limit = scanner.nextInt(); O/P:- Enter a limit: 1000 Scanner scanner = new while (left < right) {
scanner.close(); Perfect numbers up to 1000: Scanner(System.in); int temp = array[left];
System.out.println("Perfect numbers 6 System.out.println("Enter the array[left] = array[right];
up to " + limit + ":"); 28 size of the array:"); array[right] = temp;
for (int i = 1; i <= limit; i++) { 496 int size = scanner.nextInt(); left++;
if (isPerfect(i)) { int[] array = new int[size]; right--; } }
System.out.println(i); } } System.out.println("Enter the public static void printArray(int[]
} elements of the array:"); array) {
for (int i = 0; i < size; i++) { for (int i = 0; i < array.length; i+
array[i] = +) {
scanner.nextInt(); } System.out.print(array[i] + "
scanner.close(); "); }
System.out.println("Original array:"); System.out.println(); } }
printArray(array);
reverseArray(array); O/P:- Enter the size of the array:
System.out.println("Reversed 5
array:"); Enter the elements of the array:
printArray(array); 12345
}

You might also like