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

Java

Uploaded by

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

Java

Uploaded by

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

Q.What is Java?

:->Java is a high-level, object-oriented programming language developed


by Sun Microsystems (now owned by Oracle). It is platform-independent, secure, and
supports multithreading and distributed computing.
Q.What is an Exception?:->An exception in Java is an event that disrupts the normal flow
of a program's execution. It typically occurs during runtime and can be handled using try-
catch blocks to prevent program crashes.
Q.Enlist types of Inheritance?:->Single Inheritance  Multilevel Inheritance  Hierarchical
Inheritance  Multiple Inheritance (via interfaces)  Hybrid Inheritance (combination of
multiple types)
Q.What is AWT?:->AWT(Abstract Window Toolkit) is a Java library used for creating
graphical user interfaces (GUI). It contains various classes like Button, Label, and Text Field
for developing windows-based applications.
Q.State the purpose of throw keyword Ans: The throw keyword is used in Java to explicitly
throw an exception. For example: throw new Arithmetic Exception("Error message")
Q.What is Abstract class?:->An abstract class in Java is a class that cannot be instantiated
and may contain abstract methods (methods without implementation) that subclasses
must override. It can also contain concrete methods.
Q.What is an event?:->An event in Java refers to any action or occurrence recognized by
software, like button clicks or mouse movements, which can trigger a specific response
within a program.
Q.What is Method Overloading?:->Method overloading occurs when multiple methods in
the same class have the same namebut differ in the number or type of their parameters.
Q.Why Java is a architectural neutral language?:->Java is considered architectural-neutral
because compiled Java code (bytecode) can run on any system that has a Java Virtual
Machine (JVM), regardless of the underlying hardware or operating system.
Q.Define Encapsulation?:->Encapsulation is the concept of bundling data (variables) and
methods that operate on the data within a single unit, typically a class, and restricting
access to some of the object’s components using access specifiers.
Q.Explain the purpose of get Content Panel?:->To retrive the content pane of JFrame,
which is primary container where component such as buttons, panel a added.
Q.What is Java program structure?:->Java program contain many classes of which main ()
is defined in only one class./Java program structure contain following
section:/Documentation section.suggested
Documentation section <… suggested
Package statements <... optional
Import Statements <... optional
Interface Statement <... optional
class Definitions <... optional
Main Method class <... Essential
Q.Define this keyword?:->i) this keyword rafers to the current object of the class.ii) this
keyword is always reference to the object of the current class.iii) this keyword always
reference to the object.
Q.Explain in detail the data types in java?:->Data types:i) primitive.ii)Reference Data
Q.What is Interface?:->i) Interface provide blueprint for the class.ii) It support multiple
inheritance.iii) It can create interface using Interface keyword'.iv) The "implements"
keyword is used to implement an interface.
Q.What is use of Reader and Writer class?:->i) Reader class is the base class for all classes
in this group.ii) This is an abstract class.iii) Writer This abstract class represent for output.
a character stream.iv) Similer functionality like outputstream class.
Q.Which method is used to specify container layout with syntax?:->i) In java, the layout of
container is specified using layout managers.ii) Here are Some common managers in
Java:/i)Flow Layout./ii)Grid Layout./iii)Border Layout.
Q.What is the default layout for Frame and Panel?:->i) Frame (JFrame) :/The default layout
manager for a JFrame is BorderLayout.ii) Panel (JPanel):/The default layout manager for a
JPanel is FlowLayout.
Q.Explain modifier and access control used in java?:->i) Access modifier determine the
Usability 08 accessebility of classes, methods and variables.ii) There are two types of
modifiers:/i)Access modiefier./ii)Non-Access modifier.iii)) Access modifier contain private,
public and protected member.iv) Non-Access modifier contain static, final, Abstract etc
members.
Q.List and explain any two in-built exceptions?:->i) These exceptions are available in java
Liabrary which explains the error situation.ii)Various build in exception are:/i)Arithmetic
exception./ii)FileNotFoundException.i/ii)ClassNotFoundException./iii)IoException.
Q.Write a features of Java in detail?:->Features of Java:/a)Simple:i) Java is simple
language.ii)There are simple language program in java are easy to write and debug
because java use the pointer, processors, header files, operator overloading
etc./b)Secure:i) Java does not allocate direct pointer to memory./c)Portable:i)Being
Architectural neutral and having no implimentation dependent aspect of the specification
makes java portable.ii)Java code is portable./d)Object oriented:i)Java is truly object
oriented language.ii) Almost everything in java is an object./e) High Performance:i) The
Java bytecode was carefully designed so that it would be easy to translate directly into
native machine code for very high performance by using just-in-time compiler.
Q.What is polymorphism?Explain it's types?:->i)Polymorphism is ability of single entity
(such as method or an object). to take on multiple forms.ii) It allows one interface to be
used for general class of actions, enhancing flexibility and reusability in code.iii)
Polymorphism is fundamental concept of object-Oriented programming./Types of
Polymorphism./i)Compile time polymorphism: (method overloading):/i)Achieved by
defining multiple methods with the same name but different parameter lists (type,
number or both) in the same class.ii)) Determined during the compile
time.iii)Example:/class calculator{/int add (int a, int b)/{/return a+b;/}/double add
(double a,double b)/{/return a+b;/}/}/ii) Run time polymorphism (method
overriding):i)Achieved when provides a a subclass specific implimentation of method
already defined in it's parent class.ii)During runtime using dynamic method dispatch.iii)
Example:/class animal/{/void sound()/{/System.out.println("Animal makes a
sound");/}/class dog extends Animal/{/void sound ()/{/System.out.println(“dog
barks”);/}/}.
Q.What is difference between constructor and method & Explain type of constructor?:-
>Constructor:/i)Constructor is special method of class in java programming.ii) Constructor
have same name as the class itself.iii) Constructor have private, protected or
public./Method:i) A Java method is collection of statement that are group together to
perform certain task.ii) Method describes behavior of an object.iii) Method have public,
private default and protected./Types of Constructor:/Constructor/Default
constructor/Parameterized constructor/Default constructor:/i)The constructor which does
not accept Default any argument is called constructor.ii)The argument list is void.iii)
Default constructor provides the default value to the object like o, null etc. depending on
the type.iv) Syntax: Constructor Name() {}./Parameterized constructor:i) Constructor can
take value, value is called argument.ii) A constructor that has parameters is constructor.
known as parameterizd.iii) Here using parameterized constructor, it is possible to
initialized object with different set of value.iv) Syntax:/Constructor Name([parameter
List])/{/// constructor body....../}.
Q.Write a java program to calculate power of number using recursion?:->import java.util.
Scanner;/public class PowerCalculator/{/public static double/power (double base, int
exponent)/{/if (exponent == 0)/{/return 1;/}/else if (exponent > 0)/{/return base */power
(base, exponent -1);/}/else/{/return 1/power (base, -exponent);/}/}/public static void
main (string[] args)/{/Scanner scanner = new/Scanner (system.in);/System.out.println C
("Enter the base!");/double base = scanner.next.Double();/System.out.println("Enter the
exponent: ");/in't exponent = scanner.nextInt();/double result = power
(base,exponent);/System.out.println (base+"raised to the power of" + exponent + " is: “+
result);/Scanner close();/}/}.
Q.Write a java program to print the fibonacci series?:->import java.util. Scanner;/public
class FibonacciSeries/{/public static void main(String[]args)/{/Scanner Scanner =
newScanner (System.in);/System.out.println ("Enter the number of terms: ");/int n =
scanner.nextInt();/int first = 0, second 1;/System.out.println("Fibonacci series: ");/For (int
i=1; i<=n;i++)/{/System.out.println (first + " ");/int next = first + second;/first =
second;/Second = next;/}/Scanner close );/}/}.
Q.What are the rules for method overloading and method overriding?Explain it with
example?:->i)Method overloading:i)It allows a class to have multiple methods with the
same name but different parameter lists.ii)Rules:i) Return type can be different but it does
not overloading.ii) It happens within the class same.iii) It is resolved at compile-time./ii)
Method Overriding:/It allows provide a of method a subclass to Specific implementation.
defined in it's that is already parents class./Rules:/i) Method in subclass must have in
same name, parameter list and return type.ii) It happens between superclass and
subclass.iii) It is resolved at runtime.
Q. Differentiate between interface and abstract class.
Abstract Class Interface
In this abstract & non-abstract methods It can include only abstract method
can be used
Multiple inheritance not supported It supports multiple inheritance
It uses abstract Keyword It uses interface keyword.
"extend keyword is used to extend an "impliments" keyword is used to
abstract class impliment an interface
class member like private, protected etc. By default members are public
can be used
Q.Explain the concept of exception and exception handling?:->Exception:/A Java exception
is that describes an an object exceptional (that is error) condition./Exception can be
generated by the run time java system./Exceptions thrown by java relate to fundamental
errors that voilet the rules of the java language./Java exception Handling is handled by
five different keywords. These are catch, try, throw, throws, finally, keyword:/i) try:/It is as
a keyword which is utilize as program statement./ii) Catch:/It is a catch keyword used to
an exception object thrown by try block./iii) throw:/To throw exception manully, we used
throw, keyword./iv) throws :/When we want to throw an exception thrown out of a
method is written here./v) Finally:/whether an exception occurs or not, handle or not
finally block is execute
Q.What are the different types of streams ? Explain in details?:->i) A stream is nothing but
logical entity that is linked with the data Source.ii) Java I/o package contain Variety of
stream classes.iii) These classes can be catagorised into two group based On data
types:/1) Byte stream classes/2) character stream classes./1) Byte stream:/i) Byte Stream
handle reading and writting of bytes.ii) Input stream & output Stream are the abstract
classes designed for byte stream./A) Input stream:/It is an abstract class Some commonly
used subclasses include:/Bufferred Input stream/Data Input stream./B) output
stream:/This abstract class is the superclass of all classes./2) character stream:/It can be
used to read & write 16 bit unicode character Like byte stream and character stream.
classes are reader and writer stream classes:/A) Reader:/Reader class is the base class for
all classes in this group/It is an abstract class.B) Writer:/This abstract class represent a
character stream for output./Similer functinality like Output Stream class.
Q.Write a java program to calculate area 9 circle, Triangle and rectangle.(use method
overloading)?:->Public class Area Calculator/{/public double calculate Area(double
radius)/{/return math. PI * radius radius;/}/public double CalculatArea (double base,
double height)/{/return 0.5 * base * height;/}/public double Calculat Area (int length, int
breadth)/{/return length * breadth;/}/public static void main (String [] args)/{/Area
Calculator Calculator= new AreaCalculator ();/double circleArea =Calculator. Calculate
Area (5.0);double triangle Area = Calculate. calculate Area (6.0,8.0);/double
rectangleArea=calculator.calculateArea (4,7);/System.out.println ("Area of circle: "+ Circle
Area);/System.out.printen ("Area of triangle: " + triangleArea);/System.out.println( Area
of rectangle: " + rectangle Area);/}/}
Q.Explain try & catch with Example?:->a)Try:-i) Try is the keyword the statement which
you want to monitor for exception are written inside try block.ii) If any exception occure
then it is through to the Catch block.iii) Syntax:/try/{/…/…/}/b) catch:i) Catch is the
keyword Catchblock is used to catch exception which is through by try block.ii) Atleast one
catchblock after the tryblock.iii) Multiple catchblock for one tryblock.iv)
Syntax:/Catch/{/…/…/}/Example of try & catch/public class TryCatch Example/{/public
static void main (String[] args)/{/try/{/int result = 10/0;/System.out.println("Result:
"+result);/}/Catch (Arithmetic Exception)/{/System.out.println ("Error Division by Zero is
not allowed”)/}/}/}/Output: Error:/ Division by zero is not allowed.
Q.What is applet?Explain it's types?:->Applets are small java programs that are mainly
used Internet application.ii)They can be transported over the internet and can be
executed using any web browser, or appletviewer.iii) Applet does not need to execute
with main().iv) Applet is downloaded from Internet, it provide security features.v) Applet
runs in web pages./Types of Applet:/There are two types of Applets:/a)Local
Applet:i)Local Applet is written on our own, and we will embeded into web pages.ii) Local
Applet is developed locally and stored in the local system.iii) Web page doesin't need the
get information from the internet when it find the local Applet in the system.iv)
Example:/<applet code = "FirstApplet.class" height ="500" width = "500"> </applet>./b)
Remote Applet:i) Applet is create by someone else we are can access it only when
connecting with the and we internet.ii) This type of applet is stored in remote computer
and it is known as remote Applet.ii) We can download this remote and on internet
connection can execute for searching perticuler applet.iv) Example:/<applet codebase =
http://www.sarhad Code = "MyApp.class"width= 200 height=200> </applet>
Q.What is Array?Explain types of Array in details?:->Array:/i)Array is a type of group of
similer variable that are reffered to common name.ii)Arrays of any type can be created
and may have one or more dimensions./There are two types of Array:/i) single
dimensional (ID) Array.ii) Multi dimensional (2D) Array./i) single dimensional (ID) Array :i)
An array is sequence of Variables of the same data type.ii)Array have only one substrict
variable is called one-dimensional array.iii) It is also called as single- dimensional array or
Linear Array.iv) Syntax:/datatype array_name [];/ii)Multi dimensional array (2D):/i)In Java,
multi dimensional arrays are actually arrays of arrays.ii)This arrays looks and acts like
reguler multi-dimensional array.iii) To declared multi-dimensional array variable, specify
each additional index using another set of square brackets.

Q.How to create and access package in Java?Explain it with example?:->Steps to create


and access a package in Java./i)Create a Package:/i)Use the package Keyword at the top of
your Java file to declare a package.ii) Save the file in dictionary with the same name as the
package. name as the./ii) Compile the package:/Use the Javac command with the -d to
specify the destination directory for compile files./iii) Access the package:/Use the import
statement to access a package in another java file./Example:/package mypackage;/public
class calculator/{/public int add (int a, int b)/{/return a+b;/}/public int substract (int a,int
b)/{/return a-b;/}/}
Q.What is collection framework?Explain any two interfaces used in Collection
framework?:->Collection framework:/The collection framework defines several
interfaces./Collection interfaces determine the fundamental nature of collection
classes./1) Collection Interfaces:i) Collection in java represent a single unit of abject ie
group.ii) Java collection Interfaces is the root of the collection interface hierarchically.iii)
The collection interface is the foundation upon which collection Framework is built.iv) It
declare the core method that all collection will have./2) List Interface:/i) List interface
extend collection.ii) This interface has a behavior that stores a sequence of element.iii) A
List may contain duplicate elements.iv) In addition to the methods define by collection
interface, List defines its own methods.
Q.Multiple Inheritance?:->/i) Multiple inheritance rafers to a than class inherite from
more one class. (Parent class).ii) However, Java does not support multiple inheritance
using. classes to avoid 'Diamond Problem, where subclass inherits conflicting methods
from multiple parent class.iii) Java allow multiple inheritance through interfaces.iv) A class
can impliment multiple interfaces, thereby achieving multiple interfaces while
maintaining clearity & avoiding conflicts.
Q.Final Keyword?:->i) The word, 'Final' is a Keyword in Java Language.ii) The final keyword
used in several different context with the variable declaration, method 4 classes.iii) Any
final keyword when declared with variables, methods and classes specifically means:/a)
Final variable cannot be resigned once initialized.b) A final method cannot be
overridden./c) A final class cannot be extended.iv) 'Final' is a keyword which is used to
prevent method overriding.
Q.Abstract class?:->i) Abstract class is in which one a class or more methods are declared,
but not defined.ii) That means the body of these methods are omitted.Such methods are
called Abstract mrthod.iii) Abstract class cannot be instantiated Cinstantly). class that is
declared with abstract keyword, is known as Abstract class in Java.iv) Abstract class is
superclass which will shared by all of it's Subclasses.v)When class contain opne or more
abstract methods, then it should be declared abstract.vi) You must use abstract keyword
to make a class abstract.
Q.Explain java. util package?=>The java.util package is a part of the Java Standard Library
and provides a collection of utility classes that are widely used in Java programming. It
includes:/1. Collections Framework: Classes like ArrayList, LinkedList, HashMap, and
HashSet for storing and manipulating collections of objects./2.Date and Time Utilities:
Classes like Date, Calendar, and GregorianCalendar for handling date and time./3.Random
Number Generation: The Random class for generating random numbers.4.Utilities for
Working with Strings: The StringTokenizer class for parsing strings and Properties for
managing configuration properties./Example: Using ArrayList from java.util
package./import java.util.ArrayList;/public class UtilExample {/public static void
main(String[] args) {/ArrayList<String> list = new
ArrayList<>();/list.add("Apple");/list.add("Banana");/list.add("Orange");/for (String fruit:
list) {/System.out.println(fruit);/}/}/}
Q.What is Layout Manager? Explain any one in detail?=>A Layout Manager in Java is used
to define how components are arranged inside a container (like a JPanel or JFrame). It
automates the process of positioning and sizing components based on a specific layout
policy./Java provides several layout managers, such
as:/:FlowLayout/:BorderLayout/:GridLayout/:GridBagLayout/:CardLayout/FlowLayout
(Detailed Explanation):/FlowLayout is the simplest layout manager in Java. It arranges
components in a container from left to right, and when there's no space left, it moves to
the next line. It's similar to how words wrap in a text editor./:Alignment options:
FlowLayout.LEFT, FlowLayout. RIGHT,
FlowLayout.CENTER(default)./:Constructor/:FlowLayout(): Creates a layout aligned to the
center with a default 5-pixel horizontal and vertical gap./:FlowLayout(int align): Creates a
layout with a specified alignment./:FlowLayout(int align, int hgap, int vgap): Specifies
alignment and gaps.
Q.Define term finally block?=>The finally block in Java is used to execute code after a try
block, regardless of whether an exception was thrown or caught. It is typically used for
cleanup operations, such as closing files or releasing resources. The finally block ensures
that important code runs, even if an exception occurs./try{\// Code that may throw an
exception\} catch (Exception e) {\// Handle exception\} finally {\// Code that will always
execute\}
Q.What is package? Write down all the steps for package creation/=>A package in Java is a
namespace that organizes a set of related classes and interfaces. It helps to avoid naming
conflicts and can control access with visibility modifiers./Steps for Package
Creation:/1.Create a Directory: Create a directory structure that matches the package
name (e.g., com/example)./2.Write the Class: Create a Java file within the package
directory and include the package declaration at the top of the file (e.g., package
com.example;)./3.Compile the Class: Use the javac command to compile the Java file. The
compiled.class file will be placed in the package directory./4.Use the Package: To use the
package in another class, use the import statement (e.g., import com.example. MyClass;).

You might also like