We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Ql ulbich package is always imported oy defauit?
Ans. The “java.lang” gackage is always imeorted by default in Java,
QL. What ave the advantages of Packages in Tava?
Ars, Packages provide a way te onganize and cateqorize classes and
interfaces.
They hele in aveiding nawning Conflicts loetween classes and interfaces
Packaaes provide access protection by defining access control to classes and
interfaces
They facilitate modularity and reusability of code by allowing classes to be
groveed logically
Q3. what is Excegtion in Java? How can an excegtion lve handied?
Ans. An exception in Java is an event that disrurts the normal flow of the
Rragram’s instwuctions during execution Exceptions can occur due to various
YeASONS Gch OS invalid Mut, hardware failure, oF Programming errors
Excegtions can be handled using try-catch blocks. The code that might throw
an exception i6 enclosed in a try block, and covresgonding catch blocks handle
Specific tyges of excertions that might occur.
Q4. What i6 the difference between checked and unchecked excegtions?
Ans. Checked excegtionS ave checked at compile-time, and the compiler forces
the programmer to handle them using tyy-catch or Secify them in the
wmethod Signature using the “throws” Keyword
Unchecked excegtions ave not checked at comgile-time, and the grogrammer is
vot vequived to handle them. They typically vegresent grogramiming errors oFQS. What ave the 4 wes for Using exception handling with method overriding?
Ans. The overridden method in the suloclass must not throw checked exceptions
that ave broader thon the ones thrown by the overridden method in the
superclass.
The overridden method in the suboclass can throw Unchecked ercertionS or
suloclasses of the exceptions thrown by the Sugerclass method.
The overridden method in the Subclass must wot throw new or broader checked
excertions that ave vot eresent in the Suerclass methods throws clause.
TE the superclass method doen't declave any excegtions using the throws
clause, the subclass method must not declare any checked ercegtions, but it
can declare Unchecked excegtions
Qo. What is the difference between the throw and throws Keyword? Discuss
with an exawngle
Ans. ‘throw’ Keyword is used to explicitly throw an excertion within a method.
Lt is followed by an instance of the Throwable class ov its Subclasses
throws Keyword iG Used in the method Signature to declave the excertions
that the method might throw. Lt specifies the excegtions that te caller of
the method needs to handle or propagate
Examele:
11 Using, *heow Keyword
(Uolc void validateAgetint age) {
i€ loge < O £
throw new TilegaiAraumentExcegtion’Age cannot be negative");
j
jIF Sing theons Keyword
Wblic void ProcessFiledString FileName throws FileNotFoundExcertion {
FileLngutstream file = new FileLngutstveam(Filename,
1 Process the File
}
Q7. What i6 a custom exception? Why Use custom exceptions?
Aus. A custom excegtion is a user-defined excegtion class that extends from
the Excegtion class or one of its Subclasses.
Custom exceptions are used to represent specific exvor conditions or
agplication-seecific excertions that are not adequately handled by the louitt-in
exceetion classes,
They Fale in im@rouing code readability, maintainability, and allow develogers to
Convey meaningful ervor messages to users
QP. Viccuss all the common Scenarios of Java Excegtions.
Ans. Common Scenarios of Java exceptions include:
Lagut/Outgut (1/0) exceptions: Occur when theve iS an evor duving
ingutravteut operations, Such aS veading From ov writing to a File.
NulPointerExcegtion: Occurs when attemeting to access ov invoke a method on
a null obyect veFevence.
Avithmeticbxcegtion: Occuvs when an avithmetic operation results in an
Uadefined ov illegal mathematical operation, Such aS division oy Zevo.
AvvayEnderOutoBoundstxcertion: Occs when trying to access an invalid inden
of On avvay.
ClassnotfourdExcertion: Occurs when trying te load a class dynamically using its
String name lout the class defmition iS not foundInterruptedException: Occurs when a thread is interrupted while it is in a blocked or waiting state.
Q9. Why use multiple eatch blocks? Is there any possibility when the finally block is not
executed?
Ans, Multiple catch blocks are used to handle different types of exceptions that may occur within
a try block.
Each caich block can handle a specific type of exception, allowing for more precise error handling
and recovery strategies.
The finally block is always exceuted, regardless of whether an exception is thrown or caught,
unless the JVM exits abruptly (e.g., due to a system crash or by calling "System.exit()’).
Q10, What is the this keyword in Java? What are the main uses of this keyword?
Ans. The “this* keyword in Java isa reference to the current object within an instance method or
constructor.
‘The main uses of the ‘this’ keyword include:
Referring to instance variables and methods of the current object,
Differentiating between instance variables and method parameters with the same name
Invoking one constructor from another constructor within the same class (constructor chaining).
QIL. What are the main uses of the super keyword? What are the differences between this and
super keywords?
Ans, The “super’ keyword in Java is used to refer to the superclass of the current object or to call
superclass constructors and methods.
‘The main uses of the ‘super’ keyword include:
Accessing superclass members (variables and methods) that are hidden or overridden by subclass
members.
Invoking superelass constructors explicitly.cUrvent object ow to call Supewclass constructors and methods.
The main uses of the “suger Keyword include:
Accessing superclass memloers (variables and methods) that ave hidden or
evervidden loy Subclass members.
Tavoking sugerciass constwuctors exglicity
Dikferences between ‘this’ and “Super” Keywords:
“this” wefers to the current abject instance, while ‘suger wefers to the
superclass of the curvent object.
“this’ 16 used to access instance variables and methods, while ‘suger’ i6 used
to access Superclass members and constructors.
IL what i6 wnethod overloading? Why iS method overloading wot possible by
changing the vetuen tyge in Java?
Aus. Method overloading 6 a feature in Tava that allons a class to have
wultigle methods with the same name but different parameter lists.
Method overloading iS not possible boy chanaing the veturn type alone because
Tava uses the method Signature (method name and gavameter list) to
differentiate between overloaded wethods during compilation. Changing only
the vetum tyge does not change the method Signature, vesulting, ina
cowgilation eecor.
QX> what is method overriding! Differentiate between method Overleading,
and Ovewriding,
Ans, Method overriding iS a feature in Tava that allows a Sueciass to erovide
a Specific implementation of a method that is alveady defined im its
superclass,
Ditferences between methed averloading And overriding.
Method overloading occuvs within the Same class, where multigle methods havethe Same name but different gacameter lists.
Method overriding occurs in SUbclasses, where a method in the sueWclass is
vedefined with the Same signature (method name and gavameter list) in the
Suleclass.
Method overloading 5 vesolved at compile-time (Static Rolymorghisen), Laced on
the veference tye of the object:
Method overriding 6 vesclved at watime (dynamite olymorghiGe?, eased on the
actual tye of the object:
QI4. What i6 the difference between the final method and abstract method?
Ans. A Final methed i6 a method that cannot lee overridden by subclasses. I+
i declared using the “final” Keyword and grovides a Fined implementation that
Cannot lve changed loy Sueclasses.
An alostvact method iS a method declaved in an alStvact class or interface
that does not grovide an iwglementation, Subclasses ave veovived to override
alostvact methods and provide their own implementations.
The main difference 16 that a Final method cannot le overridden, while an
alostvact method must be overridden by concrete Sulciasses,
AQIS. Can we change the scove oF the overridden method in the suleciass?
Ans. No, we cannot change the Scope o€ the overridden method in the Subclass.
The Scope (access modifier) of the overridden method in the Subclass must be
The Same aS ov 125 vastvictive than the Scope of the methed in the
superclass. This ensures that the sulclass does not weaken the access control
provided by the superclass,