0% found this document useful (0 votes)
17 views10 pages

Java Programming Concepts Explained

Uploaded by

lsoni0576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Java Programming Concepts Explained

Uploaded by

lsoni0576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. What is a Local class in Java?

Ans: In Java, if we define a new class inside a particular block, it’s called a
local class. Such a class has local scope and isn’t usable outside the block
where its defined.

[Link] Java thread programming, which method is a must


implementation for all threads?
Ans: Run() is a method of Runnable interface that must be implemented by
all threads.

3. Can we have any other return type than void for main
method?
Ans: No, Java class main method can have only void return type for the
program to get successfully executed.

Nonetheless , if you absolutely must return a value to at the completion of


main method , you can use [Link](int status)

4. Can a variable be local and static at the same time?


Ans: No a variable can’t be static as well as local at the same time.
Defining a local variable as static gives compilation error

5. Is JDK required on each machine to run a Java


program?
Ans: JDK is development Kit of Java and is required for development only
and to run a Java program on a machine, JDK isn’t required. Only JRE is
required.

6. Can we use goto in Java to go to a particular line?


Ans: In Java, there is not goto keyword and java doesn’t support this
feature of going to a particular labeled line.

7. Can a constructor have different name than a Class


name in Java?
Ans: Constructor in Java must have same name as the class name and if
the name is different, it doesn’t act as a constructor and compiler thinks of it
as a normal method.
8. Can variables be used in Java without initialization?
Ans: In Java, if a variable is used in a code without prior initialization by a
valid value, program doesn’t compile and gives an error as no default value
is assigned to variables in Java.

9. A person says that he compiled a java class


successfully without even having a main method in it? Is
it possible?
Ans: main method is an entry point of Java class and is required for
execution of the program however; a class gets compiled successfully even
if it doesn’t have a main method. It can’t be run though.

10. Can we override a method by using same method


name and arguments but different return types?
Ans: The basic condition of method overriding is that method name,
arguments as well as return type must be exactly same as is that of the
method being overridden. Hence using a different return type doesn’t
override a method.

11. How can we use primitive data types as objects?


Ans: Primitive data types like int can be handled as objects by the use of
their respective wrapper classes. For example, Integer is a wrapper class
for primitive data type int. We can apply different methods to a wrapper
class, just like any other object.

12. What’s difference between Stack and Queue?


Ans: Stack and Queue both are used as placeholder for a collection of
data. The primary difference between a stack and a queue is that stack is
based on Last in First out (LIFO) principle while a queue is based on FIFO
(First In First Out) principle.

13. How can we make copy of a java object?


Ans: We can use the concept of cloning to create copy of an object. Using
clone, we create copies with the actual state of an object.

Clone() is a method of Cloneable interface and hence, Cloneable interface


needs to be implemented for making object copies.
14. What’s the purpose of using Break in each case of
Switch Statement?
Ans: Break is used after each case (except the last one) in a switch so that
code breaks after the valid case and doesn’t flow in the proceeding cases
too.

If break isn’t used after each case, all cases after the valid case also get
executed resulting in wrong results.

15. What are the two ways of implementing multi-


threading in Java?
Ans: Multi threaded applications can be developed in Java by using any of
the following two methodologies:

1) By using [Link] Interface. Classes implement this


interface to enable multi threading. There is a Run() method in this
interface which is implemented.

2) By writing a class that extend [Link] class.

16. What is multi-threading?


Ans: Multi threading is a programming concept to run multiple tasks in a
concurrent manner within a single program. Threads share same process
stack and running in parallel. It helps in performance improvement of any
program.

17. What’s the difference between an array and Vector?


Ans: An array groups data of same primitive type and is static in nature
while vectors are dynamic in nature and can hold data of different data
types.

18. Can a class have multiple constructors?


Ans: Yes, a class can have multiple constructors with different parameters.
Which constructor gets used for object creation depends on the arguments
passed while creating the objects.

19. Can we declare the main method of our class as


private?
Ans: In java, main method must be public static in order to run any
application correctly. If main method is declared as private, developer won’t
get any compilation error however, it will not get executed and will give a
runtime error.

20. What are Java Packages? What’s the significance of


packages?
Ans: In Java, package is a collection of classes and interfaces which are
bundled together as they are related to each other. Use of packages helps
developers to modularize the code and group the code for proper re-use.
Once code has been packaged in Packages, it can be imported in other
classes and used.

21. Can main() method in Java can return any data?


Ans: In java, main() method can’t return any data and hence, it’s always
declared with a void return type.

22. What is default switch case? Give example.


Ans: In a switch statement, default case is executed when no other switch
condition matches. Default case is an optional case .It can be declared only
once all other switch cases have been coded.

23. What is the difference between double and float


variables in Java?
Ans: In java, float takes 4 bytes in memory while Double takes 8 bytes in
memory. Float is single precision floating point decimal number while
Double is double precision decimal number.

24. What are Loops in Java? What are three types of


loops?
Ans: Looping is used in programming to execute a statement or a block of
statement repeatedly. There are three types of loops in Java:

1) For Loops

For loops are used in java to execute statements repeatedly for a given
number of times. For loops are used when number of times to execute the
statements is known to programmer.

2) While Loops
While loop is used when certain statements need to be executed
repeatedly until a condition is fulfilled. In while loops, condition is checked
first before execution of statements.

3) Do While Loops

Do While Loop is same as While loop with only difference that condition is
checked after execution of block of statements. Hence in case of do while
loop, statements are executed at least once.

25. What are the various access specifiers for Java


classes?
Ans: In Java, access specifiers are the keywords used before a class name
which defines the access scope. The types of access specifiers for classes
are:

1) Public: Class,Method,Field is accessible from anywhere.

2) Protected: Method,Field can be accessed from the same class to which


they belong or from the sub-classes, and from the class of same package,
but not from outside.

3) Default: Method,Field,class can be accessed only from the same


package and not from outside of it’s native package.

4) Private: Method,Field can be accessed from the same class to which


they belong.

26. What is a static variable?

In Java, we can declare a variable as static, if we declare any variable as static


the following can be done, and they are:

 A static variable can be mainly used to refer to all the common properties
of objects.
 Memory for the static variable is assigned only once in the class area at
the time of class loading

27. What is an Exception?

 An Exception handling in Java is considered an unexpected event that can


disrupt the program's normal flow. These events can be fixed through the
process of Exception Handling.
Wrapper classes in Java
The wrapper class in Java provides the mechanism to convert primitive into
object and object into primitive.

Use of Wrapper classes in Java


Java is an object-oriented programming language, so we need to deal with objects
many times like in Collections, Serialization, Synchronization, etc. Let us see the
different scenarios, where we need to use the wrapper classes.

o Change the value in Method: Java supports only call by value. So, if we
pass a primitive value, it will not change the original value. But, if we convert
the primitive value in an object, it will change the original value.
o Serialization: We need to convert the objects into streams to perform the
serialization. If we have a primitive value, we can convert it in objects
through the wrapper classes.
o Synchronization: Java synchronization works with objects in Multithreading.
o [Link] package: The [Link] package provides the utility classes to deal
with objects.
o Collection Framework: Java collection framework works with objects only.
All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet,
LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects
only.

Primitive Type Wrapper class

boolean Boolean

char Character

byte Byte

short Short

int Integer

long Long
float Float

double Double

Difference between object and class


There are many differences between object and class. A list of differences between
object and class are given below:

No Object Class
.

1) Object is an instance of a class. Class is a blueprint or


template from which
objects are created.

2) Object is a real world entity such as pen, Class is a group of


laptop, mobile, bed, keyboard, mouse, chair etc. similar objects.

3) Object is a physical entity. Class is a logical entity.

4) Object is created through new keyword mainly Class is declared


e.g. using class keyword e.g.
Student s1=new Student(); class Student{}

5) Object is created many times as per Class is declared once.


requirement.

6) Object allocates memory when it is created. Class doesn't allocated


memory when it is
created.

7) There are many ways to create object in java There is only one way to
such as new keyword, newInstance() method, define class in java using
clone() method, factory method and class keyword.
deserialization.

String manipulation: A String is an unavoidable type of variable while writing any


application program. String references are used to store various attributes like
username, password, etc. In Java, String objects are immutable. Immutable
simply means unmodifiable or unchangeable.

Once String object is created its data or state can't be changed but a new String
object is created.

program to add string:

1. class Testimmutablestring1{
2. public static void main(String args[]){
3. String s="Sachin";
4. s=[Link](" Tendulkar");
5. [Link](s);
6. }
7. }

Match the string:


1. class Teststringcomparison1{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3=new String("Sachin");
6. String s4="Saurav";
7. [Link]([Link](s2));//true
8. [Link]([Link](s3));//true
9. [Link]([Link](s4));//false
10. }
11.}
12.
13.

14.
15.
16.
17.

18.

19.

You might also like