JAVA Important Questions and Answers
JAVA Important Questions and Answers
a) Local Variables
b) Global variables
The process of converting one primitive data type into another primitive data type is known as primitive
typecasting.
Widening: The process of converting smaller data type into larger data type is known as widening. There is
no any data loss in widening so compiler going to do it implicitly. It is also called as auto widening.
Narrowing: The process of converting larger data type into smaller data type is known as narrowing. There
is no any data type so compiler will not do it implicitly. The programmer has to do it implicitly.
Based on number of operands which an operator accepts, it is classified into three types
1. Unary operators:
Unary operator accepts only single operand
Ex. Typecast operator, Logical NOT operator
2. Binary operators:
Binary operator accepts two operands
Ex. Arithmetical operators, Relational operators, Logical operators
3. Ternary Operators:
An operator which accepts three operands is known as ternary operators.
Ex. Conditional operator
6) What is break?
Break is a keyword.
Break is a control transfer statement; it transfers the control outside of the block.
Break can be used inside the switch and loops.
7) What is loop?
Whenever set of instruction or same set of statements are repeated multiple time then we use loops.
1. Initialization
2. Condition
3. Updation
8) What is method?
Method is a block of instruction to perform specific task.
Methods will get execute only when it is called.
Syntax:
Method Defination: Method declaration along with method implementation is called as method defination.
There are two types of methods –
1. No Argument Method: A method which will not accept any argument is known as no argument method.
2. Parameterized Method: A method which is declared with formal arguments is known as parameterized
method.
9) What is return?
Return is a keyword.
It is control transfer.
If there is any statement within the block, we will get CTE as unreachable statement.
Return type: The type of value return to its caller is known as return type.
1) Primitive types
2) Non Primitive types
3) Void:
a. Void is a keyword
b. It represents data type (Nothing)
c. If the return type of method is void it is not mandatory to use return statement.
Collection Framework Page 3
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
9) What is method overloading?
- The process of creating more than one method with a same name but different formal arguments is known as
method overloading.
-The formal arguments must be differing either in length or in type or in the order of declaration.
Compiler achieves the binding between method call statement and method implementation to be executed at
compile time based on actual arguments passed this is known as compile time binding.
- Static is a modifier
- Any member of a class prefix with static modifier is known as class member / static members.
A. Static Methods:
Static methods can be called in two ways within the same class.
B. Static Variable:
- A global variable which is prefixed with static modifier is known as static variable.
* Static variable‟s memory is allocated in class static area hence it is also known as class variable .
* A static variable will be assigned with default value hence it can be used without initialization.
Collection Framework Page 4
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
* A static variables can be used within the same class directly or with the help of class name.
b. Static block
* The static initializers get executed during the loading process of class (that is before main method is called)
* Static initializers get executed only once for each time when class is loaded.
3. If any static methods are present then those static method signature along with the reference (address)
should load inside class static area.
4. If any static variable are present then, those variable should load with default values inside Class static
area.
5. If any initializers are there then they get executed. Then the loading process of class is said to be
completed.
- With the help of „new‟ keyword we can create a block of memory in heap area. New operator returns the
address of an object created. Every time new operator is used a new object is going to create and address
will be different.
* A global variable which is not prefixed with static modifier is known as non static variable.
* Non static variable will be allocated inside the object of a class which is created
* Non static member can not be used directly or with the help of class name inside static context.
* We can use the non static members from static context only with the help of reference. (Address of an
object)
* Non static variable will have one copy each object of the class created, therefore if we created n number of
objects for a class we will be having n copies of non static variable (One in each object)
Collection Framework Page 5
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
B. Non static Methods:
* A method which is not prefixed with static modifier is known as non static method.
* The block which is belongs to non static method is known as non static context.
* We cannot call non static method from static context directly; we get CTE.
* Any block which belongs to non-static members is known as non static context. Inside non static context
we can use static members of class directly, We can use non static members of same class directly.
* Non static initializers are gets executed during loading process of an object.
* It will get execute for each and every object during its loading process.
A non static initializer (block) gets executed only once for each object.
*We can use non static block to count the number of objects created for a class.
2. his is a non static variable which will have the address of object currently used Address of currently used
5. If a local variable name and object variable name (non static variable name) is same we can use the object
variable with the help of this keyword
A. No argument constructor
* The no argument constructor added by compiler while compilation this known as default constructor.
* Compiler will add default constructor only when the constructor are not defined by the programmer.
The compiler would add instructions which helps to load the non static members of the class into object.
Collection Framework Page 6
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
The non static initializers written by programmer will added inside constructor body by the compiler.
Constructors can be defined by programmer & it is known as user defined constructor.
B) Parameterized Constructor:
- A constructor which accepts arguments is known as parameterized constructor.
- Parameterized constructor are used to initialize the states of an object during the loading process (Object
creation)
5. The user written instructions of constructor gets executed once the execution of constructor is done, then
the reference of object will be return. Then the loading process of object is said to be done.
2. Constructor declaration must change in its declaration that is in the length or type or order of formal
arguments.
Constructor chaining:
A. This call statement: It is used to achieve constructor chaining within same class.
Rules:
1. This call statement can be used only inside a constructor
2. It should always be the first statement inside constructor body
3. This call statement should not call constructor recursively.
Collection Framework Page 7
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
4. If a class has n number of constructors we can use this call statements only in n-1 number of
constructors
B. Super call statement: It is used to call the constructor of superclass from subclass.
Rules:
1. super call statement can be used only inside a constructor
2. It should always be the first statement inside constructor body
3. We cannot use both super() and this() in one constructor.
PART: 2
1) What is Encapsulation?
* Encapsulation is the process of binding the states and behaviors of an object.
Data hiding is the process of restricting the direct access to the states of an object and providing the controlled
access with the help of behavior of the same object(class) is known as data hiding.
*Prefix the state of an object with the help of private access modifier.
*Define getter and setter method to access private data member.
*Getter method is used to fetch the private data member from a different class.
*Setter method is used to modify the private data member of a class from a different class.
We represents the states of an object with the help of non-static variable and behavior of an object with the help of
non-static methods.
EXAMPLE:
{
int acno;
String name;
double balance;
String ifsc;
SavingAccount (){}
this.acno=acno;
this.name=name;
this.balance=balance;
this.ifsc=ifsc;
}
if(amt<=balance)
{
balance=balance-amt;
else
{
sopln("INSUFFICIENT BALANCE");
balance=balance+amt;
{
sopln("Account Details");
//Driver class
class SavingAcountDriver
{
SavingAccount s1=newSavingAccount(123,"Dinga",30000,SBIN00041114);
s1.display();
s1.withdrawn(5000);
s1.deposit(10000);
Example:
*Car is a vehicle.
Collection Framework Page
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
*QSPIDER is an institute.
*Peacock is a bird.
*Dog is an animal.
INHERITANCE:
>>Inheritance is a design techinque which is used to establish IS-A relationship,it is a process where one type of
object acquires the properties and behaviours of another type of object.
PARENT CLASS:
>>The class which provides the states and behaviours to another class is known as parent class.
Parent class is also known as Super class/base class/Generalised class.
CHILD CLASS:
>>The class which recieves the states and behaviours from parent class is known as child class.
Arrow head represents the super class,and arrow tail represents the sub class.
Let us consider only accounts and loan accounts,Here loan a/c will have inherited members (members of a/c class)
as well as declared in Loan a/c.Therefore the states of Loan accounts are
>a/c number
>a/c name
>balance
>ifsc code
>Loan a/c
>Interest rate.............
8)How do we achieve Inheritance b/w two classes in java. Explain with example.
In java,We achieve is A relation with help of keywords
*extends
*implements.
SYNTAX:
class Parent
{
{
}
EXAMPLE:
class BankAccount
{
int acno;
String name;
double balance;
String ifsc;
BankAccount(){}
{
this.acno=acno;
this.name=name;
this.balance=balance;
this.ifsc=ifsc;
}
sopln("Account Details");
sopln("Account Number :"+acno);
//inheriting
class LoanAccount extends BankAccount
double loanamt;
double interest;
LoanAccount()
{}
this.acno=acno;
this.name=name;
this.balance=balance;
this.ifsc=ifsc;
this.loanamt=loanamt;
this.interest=interest;
}
sopln("Account Details");
sopln("Account Number :"+acno);
}
//Driver class
class BankDriver
{
public static void main(String[]args)
b1.accountDetails();
}
//Loan account
class LoanDriver
{
l1.LoandAccountDetails();
}
}
8) What is extends?
>> extends is a keyword.
>>A class can extends to only one class. It cannot extends more than one class.
>>extends keyword should be used in SUB class.
Hierarcheal Inheritance:
Collection Framework Page
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
If a super class has more than one sub class in same level is known as Hierarcheal Inheritance.
Multiple Inheritance:
A sub class having more than one super class at the same level is known as Multip le Inheritance.
Hybrid Inheritance:
10)Why cant we achieve multiple inheritance with the help of classes in java?
Multiple inheritance has an ambiguity problem is known as diamond problem.
Because of this diamond problem in java, we cant achieve multiple inheritance only with the help of classes.
A class can in java can inherit only with the help of classes.
Collection Framework Page
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
Case 1:
>>When we create an object for class C,during the object loading process of object C,the constructor of class C tries
to call the constructor of the super class,with the help of super() statement .
>>At this situation,there is an ambiguity problem to call either constructor of class A or contructor of class B.it is
known as Diamond problem.
Case 2:
>>Assume both the superclass have methods with same signature,if we tries to call the method with the help of sub
class referrence,there is an ambiguity weather to execute implementation of class A method or to execute the
implementation of class B method.
>>In the loading process of a class,parent class is loaded first and then the sub class or child class gets loaded.
>>Sub class static area will be have a referrence to the super class static area,Therefore
a)In sub class we can use static members of sub class directly(inherited).
b)With the help of sub class name,we can use static members of sub class as well as super class.
Example:
class A
sopln(a);//10
>>In the sub class we can use non-static members of super class.
>>With the help of sub class object refference,we can use the members of sub class as well as super class.
>>With the help of super class object refference We can use only the member of super class and we cannot use the
members of sub class.
>>This is done with the help of constructor chaining using super call statement.
EXAMPLE:
class A
{
int a=10;
{
public void test()
sopln("a :"+a );
}
}
Class Driver1
{
Public static void main(String[]args)
B obj=new B();
obj.test();
sopln(obj.i);
14)What is package?
Package is a mechanism which is used to store group of similar java files together.
>>Java package is used to categorize the classes and interfaces so that they can be easily maintained.
In java, we can only use a public member outside package. A non- public member cannot be used outside the
package.
RULE:
1)Fully qualified name: The member name along with the package name is known as fully qualified name.
2) Import keyword: it is used to load a class from a specified package into the current package.
16)What is import?
>import is a keyword.
>It is used to load a class from specified package into the current package.
17)Can we use the members of a package outside a package without using import.?
Yes, we can access the public member outside a package without import keyword.
Ex:packagename.classname.member name;
Access Modifiers:
1)Private:
Private is an access modifier,any member of a class which is prefixed with private keyword is known as private
member.
>>It cannot be used in different class or sub class present in same package and it cannot be used in different class or
sub class present outside the package.
2) Default/package scope:
If a member is not prefixed with any access modifier,then it is known as default scope or package scope.
>>A member with a package scope can be used within the same class or sub class or different class present in the
same package.
>>It cannot be used in different class or sub class present outside a package.hence it is known as package scope.
3) Protected:
Protected is an access modifier, any member of a class which is prefixed with protected modifier is known as
protected member.
>>It can be used within the same class, different class or sub class present inside a same package and it is also can
be used in sub class of different package.
>>It cannot be used in a different class present outside a package.
4)Public:
It is an access modifier.
>>Any member of a class which is prefixed with public keyword then it is known as public member.
>>It cannot be used in same class,different class,sub class of same package as well as in different package.
If we try to extend a class which is having private constructor, compile time error will occur. A private constructor
doesn‟t allow a class to be sub classes.
POLYMORPHISM:
Compile time binding is done with the help of method signature. This is known as Compile time polymorphis m.
1)Method overloading.
2)Constructor overloading.
It is also known as Non-static binding.In run time polymorphism,the bind between method call statement and
method implementation happening during run time,hence it is known as run time polymorphism.
>>We can achieve run time polymorphism with the help of Method overriding.
21)Explain Shadowing?
Shadowing:
Variable shadowing:
Collection Framework Page
Akash V Qspiders Basavanagudi [Insta_id : qspiders_basavanagudi]
If the super class and sub class having either static or non static variable with the same name,it is known as variable
shadowing.
>>It is the compiler who decides which variable to be used based on
**Based on invocation/usage.
Method Shadowing:
If the super class and the sub class having the static methods with same signature,it is known as method signature.
Rules:
>>Place of invocation/usage.
22)What is Overriding?
The process of providing an implementation to super class method with the help of sub class is known as Method
overriding.
>>In the sub class,non static method signature should be same as super class non static method.
>>NOTE:Which method implementation to be executed will be decide on object creation,and not depend on
referrence variable.
Static methods cant be overridden.
*Methods
*Class
Final Variable:
>>A variable which is prefixed with final modifier is known as final variable.
Final Methods:
If a method is prefixed with final modifier then it is known as final method.
Final class:
24)Explain Abstraction?
It is one of the oop‟s design technique,which helps us to provide only the essential
feautures of an object and hide their implementation details from the user.
2)Abstract method
Concrete Method:
A method which does‟t have method body or implementation is known as abstract method.
1)Abstract class
Abstract class:
>>Abstract class cannot be instantiated.(We cannot create an object for abstract class)
Concrete class:
A class which is not prefixed with abstract modifier Is known as concrete method.
Note:
>>Static methods cannot be made as abstract method.
26)What is an Interface?
Interface is a non-primitive user defined datatype which is used to achieve 100% abstraction.
>>We cannot make an interface as private or protected,it can be either public or default.
Members of interface:
>>Constructors
>>Non static concrete methods
>>Private methods
>>Protected methods
PART : 3
2) What is mean by array?
-Arrays is a continuous block of memory which is used to store homogeneous data/values.
-The values stored inside array is known as elements.
-In java array is an object.
-Array reference variable is used to store the address of array object.
-The default value of array reference variable is null. Therefore inside the reference variable of an array we
can store either null or address of respective array objects.
Multidimensional arrays are nothing but an array with more than one dimension ie more than one row or one
column.
1) 2-Dimensional array
2) 3-Dimensional array
2-Dimensional Array:
“Jagged Arrays”.
1) Jagged arrays are the arrays which consists of elements of different sizes in each array.
2) While declaring jagged arrays, it is not required to mention the size of secondary array.
Limitations of Array :
The size of the array is fixed, we cannot reduce or increase dynamically during the execution of the
program.
Array is a collection of homogeneous elements.
Array manipulation such as :
- removing an element from an array.
- adding the element in between the array etc…
Requires complex logic to solve.
4) What is Collection?
Collection Framework is set of classes and interfaces ( hierarchies ), which provides mechanism to store
group of objects ( elements ) together.
Constructors:
7) What is iterator?
iterator() is method which belongs to Iterable Interface.
iterator() method creates an Iterator type object and returns the reference.
1. hasNext() : it checks whether there is an element to be accessed from the collection, if present it
returns true, else it returns false.
2. next() : it is used to access the element, and moves the cursor to the next element. The return
type of next() is Object.
9) What is ListIterator()?
listIterator() :
listIterator() is a method that belongs to List interface, listIterator method creates an object
of ListIterator type.
ListIterator :
Characteristics Of Set :
Characteristics of list:
The String object created using new will be stored inside heap area & not in the string pool area.
1) toCharArray()
2) charAt(int index)
toCharArray()
it is a non-static method of string class which is used to convert a string into an array of
characters.
charAt(int index):
it is a non-static method of string class which is used to access the characters present inside the
string. The charAt() method will return the character present in given index.
StringBuilder():
StringBuilder(int capacity):
StringBuilder(String str):
In order to avoid this whenever the string involves many manipulation in the application.
It is recommended to store strings using either string buffer or string builder.