java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Mohd Huzaif

JAVA

1. What is Java?
Java is a high-level, object-oriented programming language.
2. What are JDK, JRE & JVM? How a Java program compiled or executed? V.
IMP.
JDK (Java Development Kit): Includes tools for Java applica on
development, such as javac, jheap, jconsole etc.
javac tool compile your java file into bytecode (.class file)
JRE (Java Run me Environment): Provides the run me environment for
execu ng Java applica ons, including the JVM.
contains Java class libraries/Java API which assist your java file to
execute.
JVM (Java Virtual Machine): Executes Java bytecode, transla ng it into
na ve machine code. Also do memory management etc.
JIT (Just-In-Time) compiler is a component of the JVM that compiles Java
bytecode into na ve machine code at run me.
Mohd Huzaif

3. What is compile- me and run- me in Java?


Compile Time: Compile me is the phase during which the Java source
code is translated into bytecode by the Java compiler (javac).
Run me: Run me refers to the phase during which the Java Virtual
Machine (JVM) executes the compiled bytecode.
4. What are the features and advantages of Java?

5. How Java is pla orm independent? Why convert java code to bytecode?
Java achieves pla orm independence by compiling source code into
bytecode, which can run on any pla orm with a compa ble JVM.
6. What is main method in Java? What is the role of public, sta c and void in it?
Mohd Huzaif

7. What is Java Bytecode? What is high-level, low-level code?


Java bytecode is a pla orm-independent intermediate code generated
by the Java compiler(javac).

8. What are variables & data types? What are the types of data ty
Variables are used to store data.
Data types define the type of variable.
Mohd Huzaif

9. What are reference/ non-primi ve data types? V. IMP.


Reference (or non-primi ve) data types in Java are data types that do not
store the actual data directly, but instead store references (heap memory
addresses) to values in memory.

10. What are the differences between primi ve and reference data types?
Mohd Huzaif

11. What are Operators? What the types of operators in Java?


Operators are symbols or keywords used to perform opera ons on
operands.

12. What are control statements in Java? V. IMP.


Control statements manage the flow of execu on in a program
Mohd Huzaif

13. What is the difference between break and con nue statement? V. IMP.
The break statement is used to terminate the loop completely.
The con nue statement is used to skip the current itera on of the loop
and move on to the next itera on of the loop.

14. What is OOPS? What are the main concepts of OOPS? V. IMP.
OOP stands for Object-Oriented Programming, which means OOPS is a
way to create so ware around objects
OOPs provide a clear structure for the so ware's and web applica ons.

15. What are the members of class?


Mohd Huzaif

16. What is the role and benefit of package in Java?


Defini on: Packages create a namespace that helps in organizing classes
or interfaces.
Benefit: This avoids naming conflicts by allowing the same class name to
be used in different packages.

17. What isStringBuilder? What is the difference between String and


StringBuilder?
String is immutable. It means if you defined one string then you couldn’t
modify it. Every me you will assign some value to it, a new string is
created.
StringBuilder is mutable. It means if you defined one string then you can
modify it and a new string is not created.
Mohd Huzaif

18. When to use String and when to use StringBuilder in real applica ons.
Use String for constant values because Strings are light weight and
therefore improve performance.
Use StringBuilder when concatena ng or modifying strings frequently.

19. What are access specifiers? What are public and private specifiers? V. IMP.
Access specifiers (access modifiers) are keywords used to set the access
level for classes, variables(fields), methods, and constructors.

20. What are ge er and se er methods?


Ge er methods are used to retrieve the values of private fields of a class.
Se er methods are used to modify or set the value of a private field of a
class.

21. What is the role of default access specifier?


The default access specifier(package-private access) is used when no
explicit access specifier is provided. It controls the accessibility of
member within the same package.
Mohd Huzaif

22. What is the role of this keyword in java? When to use it? V. IMP.
this keyword refers to the current instance of the class.
Use of this keywords: Differen ate between instance variables and
parameters with the same name in se er methods.

23. Why to use same names for class fields and parameter name in Se er
method?
Using the same names for class fields and parameter names in se er
methods can lead to cleaner, more readable code.

24. What are the advantages of ge er and se er methods?


1. Data Valida on: Se er methods can include valida on logic to ensure
that the assigned values are valid.
2. Data Access: Ge er methods can control how a field is accessed.
3. Encapsula on/ Abstrac on: Ge er and se er methods hide the
internal implementa on details of a class and expose only necessary
informa on.
Mohd Huzaif

25. What is inheritance and when to use inheritance in real applica ons?
Defini on: Inheritance is crea ng a parent-child rela onship between
two classes, where child class will automa cally get the proper es and
methods of the parent.
When to use of inheritance?
Code Reusability: Mul ple Subclasses can reuse fields and methods from
a superclass.

26. How to implement inheritance in Java? V. IMP.


Mohd Huzaif

27. Why Java does not support mul ple inheritance of classes? What is
diamond problem?
Mul ple inheritance can lead to the "diamond problem". In diamond
problem, confusion arises if a child class inherits from two parent classes
that have a same name method.

28. What is the alterna ve of mul ple inheritance in Java? V. IMP.


Java support mul ple inheritance of interfaces. So mul ple interfaces
and maximum one parent class can be inherited.
Mohd Huzaif

29. How to prevent a class from being inherited?


In Java, you can prevent a class from being inherited (subclassed) by
using the final keyword.

30. What is Method Overloading? How to implement it and when to use it?
Method Overloading occurs when there are mul ple methods with the
same name within the same class but with different parameter lists.
Mohd Huzaif

31. Why do we call method overloading as a type of compile- me or early binc


Method overloading in Java is referred to as compile- me polymorphism
(or early binding) because the decision of which method to call happens
at compile me based on the method signature(method parameters),
before the program is executed.

32. In how many ways can a method be overloaded? V. IMP.


1. Method name same, but data types of parameters are different.
2. Method name same, but number of parameters are different.
3. Method name same, data type of parameters are same, but the order
of parameters is different.
Ex: add(int a, double b) add (double a, int b)

33. What is Method Overriding?


Method overriding in Java allows a subclass to provide a specific
implementa on of a method that is already defined in its superclass.

34. Why to use method overriding? Why don't we have different name
methods?
Method-overriding concept for making our applica on more structured,
readable, and organized.
Mohd Huzaif

35. Why we call method overriding as a run- me or late binding?


Method overriding in Java is called run- me polymorphism (or late
binding) because the decision of which method to execute is made
dynamically at run me based on the actual object.

36. What are the 5 differences between Overloading and Overriding? V. IMP.
Mohd Huzaif

37. What are Annota ons in Java?


Annota ons in Java are a form of metadata that provide informa on
about a program/func on to the compiler.
Annota ons are declared using the @ symbol

38. How to achieve Encapsula on in Java? V. IMP.


Encapsula on can be achieved by using access modifier(private) and
ge ers and se ers methods.
Mohd Huzaif

39. What are the advantages of Encapsula on in Java?

40. What is Abstrac on? How to implement abstrac on?


Abstrac on means showing only required things and hide the
background(complex implementa on) details.
Mostly we use abstract classes and interfaces for implemen ng
abstrac on.
Mohd Huzaif

41. What is the difference between abstrac on and encapsula on? V. IMP

42. What is abstract class In Java? How to implement it?


Abstract classes contain both abstract and concrete methods and
abstract class object crea on is not possible.
Abstract methods of abstract class must be implemented by child class.
Mohd Huzaif

43. When to use abstract class in real applica ons? V. IMP.


Abstract class is a good choice when you are sure some methods are
concrete/defined and must be implemented in the same way in all
derived classes. And some methods are abstract and can be
implemented differently by implemen ng classes.

44. What are interfaces in Java? How to implement it?


Interfaces contain abstract methods which must be implemented in the
implemen ng class.
Two points about Interfaces:
1 Interfaces define a contract(rules) for implemen ng classes.
2 Mul ple Inheritance via Interfaces is possible.
Mohd Huzaif

45. When to use interfaces in real applica ons?


An interface can be used to define contracts which the implemen ng
class must implement. This will make classes more modular and
structured.

46. What are the differences between an Abstract class & an Interface?
Mohd Huzaif

47. What are default methods? When to use default methods?


Defini on: Default methods are methods in interfaces that have a
default implementa on.
They are declared using the default keyword.
Purpose: Default methods in Java interfaces introduced in Java 8 provide
a way to add concrete method implementa ons to interfaces, avoiding
the need to convert interfaces into abstract classes for such purposes.

48. Can you create an instance of an Abstract class or an Interface?


No, crea ng instance of abstract class and interface is not possible.

49. Do abstract class can have Constructors? What is the use of that
constructor?
Mohd Huzaif

50. Do Interface can have a constructor?


NO, Interface can not have constructors.

51. When to use Interface and when Abstract class in real applica ons?

52. How to achieve abstrac on? Difference between abstrac on and abstract
class?Difference between abstrac on and abstract class?
Abstrac on is a broader concept of hiding complex implementa on
details and showing only the essen al features, whereas abstract class is
a prac cal way to implement Abstrac on.
Mohd Huzaif

53. What is Constructor in Java?


A constructor is a special type of method that is called when the object
of class is created.
A constructor has the same name as the class and does not have a return
type, not even void.

54. What are the types of constructors? What is Default constructor?


A default constructor in Java is a constructor which is automa cally
provided by Java if no other constructors are explicitly defined.

55. What is Parameterized constructor? When to use it in real applica ons?


Defini on: A parameterized constructor is a constructor with
parameters.
Use: It is used to ini alize the specific field values of the class provided
during object crea on.
Mohd Huzaif

56. What is constructor overloading? When to use it in real applica ons?


Defini on: Constructor overloading is the concept of having mul ple
constructors within the same class, each with a different parameter list.
Use: Constructor overloading enhances the usability of classes by
providing mul ple ways to ini alize objects based on varying
requirements and input parameters.
Mohd Huzaif

57. What is constructor chaining?


Constructor chaining is the process of one constructor calling another
constructor from the same class.

58. What is copy constructor?

59. What is the role of super keyword?


Super() keyword is used to call the constructor of superclass from
subclass constructor, but it is op onal to write.
Mohd Huzaif

60. What is Excep on Handling? How to implement it in Java? V. IMP.


Excep on handling in Object-Oriented Programming is used to MANAGE
ERRORS.

61. What is the role of finally in excep on handling? V. IMP.


The finally block is used to execute a given set of statements, whether an
excep on occur or not.

62. When to use finally in real applica ons?


Finally block is mostly used for cleaning up resources.
For example:
Mohd Huzaif

63. Can we have mul ple catch blocks and when should we use mul ple catch
blocks?
Mul ple catch blocks are used to:

64. What is catch-all block? Is it a good prac ce in real applica ons?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy