0% found this document useful (0 votes)
3 views17 pages

F Also S

The document provides a comprehensive overview of Java concepts, including access modifiers, data types, variables, coding standards, inheritance, polymorphism, abstraction, exception handling, and collections. It explains key features, differences, and usage of various Java elements such as classes, methods, and keywords. Additionally, it touches on Java 8+ concepts like lambda expressions and the Stream API.
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)
3 views17 pages

F Also S

The document provides a comprehensive overview of Java concepts, including access modifiers, data types, variables, coding standards, inheritance, polymorphism, abstraction, exception handling, and collections. It explains key features, differences, and usage of various Java elements such as classes, methods, and keywords. Additionally, it touches on Java 8+ concepts like lambda expressions and the Stream API.
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
You are on page 1/ 17

Here is a simple and clear answer for each question you've listed, organized under

headings:

🔐 Access Modifiers in Java


1. What are the different access modifiers in Java?

 private: accessible only within the same class


 (default): accessible within the same package
 protected: accessible within the same package and in
subclasses (even in other packages)
 public: accessible from anywhere

2. What is the default access modifier in Java?

 When no modifier is written, it's package-private (also called


default access).

3. Can a class be declared protected or private? Why or why


not?

 No, top-level classes cannot be private or protected.


o Only public or default access is allowed.
o Reason: If a class is private or protected, other classes can't
access it, so it becomes useless.

4. Difference between protected and package-private?


Accessible in Same Accessible in Subclasses in
Modifier
Package Other Packages
package-private
✅ Yes ❌ No
(default)

protected ✅ Yes ✅ Yes (if subclassed)


5. Accessibility summary:
Same Same Subclass (Other
Modifier Other Package
Class Package Package)

private ✅ ❌ ❌ ❌

default ✅ ✅ ❌ ❌

❌ (only through
protected ✅ ✅ ✅
inheritance)

public ✅ ✅ ✅ ✅

📦 Data Types in Java


6. Two main categories of data types:

 Primitive types (e.g., int, boolean, char, etc.)


 Reference types (e.g., String, arrays, objects)

7. Default values:

 boolean: false
 int: 0
 Object: null

8. Why is String not a primitive type?

 Because String is a class, not a basic data type.


 It allows many features (like methods .length(), .substring()), which
primitive types don’t have.

9. Difference between float and double:


Featur
float double
e

Size 32-bit 64-bit

Precisio
~7 digits ~15 digits
n
Featur
float double
e

f (e.g., none or d (1.5 or


Suffix
1.5f) 1.5d)

10. Wrapper classes:

Java provides wrapper classes to use primitive values as objects:

 int → Integer
 boolean → Boolean
 char → Character
 double → Double, etc.

🔧 Variables in Java
11. Types of variables:

 Instance variables (belong to objects)


 Static variables (belong to class)
 Local variables (defined inside methods)

12. Instance vs Local variables:


Type Where declared Lifetime

Instanc Inside class, outside As long as object


e method exists

Only while method


Local Inside method
runs

13. Static variable:

 Shared by all objects of the class.


 Use when a value is common to all instances (e.g., count of
objects created).
14. Scope/lifetime of local variable:

 Exists only inside the method where it is declared.


 Destroyed after method completes.

15. Can static method access instance variables directly?

 ❌ No, because static methods belong to the class, not to an object.


 They can only access static variables.

16. Primitive vs Reference variables:


Type Stores Example

Primitive Actual value int x = 5;

Referenc Memory address of


String s = "Hi";
e object

17. Primitive types in Java:

 byte, short, int, long


 float, double
 char
 boolean

18. int vs Integer:

 int is a primitive.
 Integer is a wrapper class (object) for int.

19. How are reference variables stored?

 Variable stores the address (reference) to the object in memory.

20. What happens if reference variable is null?

 It means it doesn’t point to any object.


 Trying to use it → causes NullPointerException.

21. Can a reference variable point to a primitive?

 ❌ No. Reference variables point to objects only, not primitive types.


 But wrapper classes can be used instead (e.g., Integer x = 5;)

✍️Coding Standards
22. Common coding/naming conventions:

 Class names: PascalCase (e.g., StudentInfo)


 Method/variable names: camelCase (e.g., getName())
 Constants: ALL_CAPS_WITH_UNDERSCORE (e.g., MAX_SPEED)

23. How to name:

 Class → Car, StudentProfile


 Method → getDetails()
 Variable → speed, userName
 Constant → MAX_LIMIT

24. How to declare constant in Java?


public static final int MAX_USERS = 100;

25. Purpose of final keyword:

 Makes variable unchangeable (constant)


 Can also be used with methods/classes to prevent overriding

26. What if we reassign a final variable?

 ❌ Error! Cannot reassign value to a final variable.


27. Can final reference variable point to another object?

 ❌ No, you cannot change the reference.


 But you can change the object’s internal data.

👨‍👩‍👧‍👦 Inheritance in Java


28. What is inheritance?

 When one class (child/subclass) gets properties and methods


from another class (parent/superclass)

29. Types of inheritance in Java:

 Single
 Multilevel
 Hierarchical
 ❌ Java does not support multiple inheritance with classes

30. Why no multiple inheritance (classes)?

 To avoid ambiguity (e.g., same method in 2 superclasses)


 Java supports it using interfaces

31. "is-a" vs "has-a"


Type Example Meaning

Inheritance (Car is a
is-a Car extends Vehicle
Vehicle)

Composition (Car uses


has-a Car has Engine
Engine)

32. Inheritance vs Composition


Inheritance Composition

"is-a" "has-a"

Uses extends Uses object inside


Inheritance Composition

class

Tightly
Flexible
coupled

33. Can constructors be inherited?

 ❌ No. But the child class can call the parent constructor using
super().

34. Can subclass override static method?

 ❌ No. Static methods belong to the class, not the object.


 You can hide a static method, but not truly override.

35. Can private members be inherited?

 ✅ They are inherited, but not accessible in the child class.


 You can access them only via public/protected getters/setters
in the parent class.

Let me know if you want these in PDF format or MCQ quiz form to revise easily!

Great! Let's continue with more core Java OOP and concept questions — I’ll keep each
answer short and clear:

🔄 Polymorphism in Java
36. What is Polymorphism?

 “Many forms” — same method name behaves differently


depending on object or context.

37. Types of Polymorphism:

 Compile-time (Static) → Method overloading


 Runtime (Dynamic) → Method overriding
38. Method Overloading vs Overriding:
Overloadi
Feature Overriding
ng

Compile
When? Runtime
time

In same class? Yes No (subclass involved)

Signature Yes Same name, same


difference? (params) params

Custom behavior in child


Use Flexibility
class

39. Can static methods be overloaded/overridden?

 Overloaded → ✅ Yes
 Overridden → ❌ No (they are hidden, not overridden)

🔧 Abstraction and Interfaces


40. What is abstraction?

 Hiding internal details, showing only essential features.


 Done using abstract classes or interfaces.

41. Abstract class vs Interface:


Feature Abstract Class Interface

Can have abstract + All methods are abstract


Methods
normal (Java 7)

Constructors Yes ❌ No

Multiple
❌ No (single class) ✅ Yes (multiple interfaces)
inherit?
42. When to use abstract class?

 When you want to provide partial implementation and share


code.

43. When to use interface?

 When you want 100% abstraction or multiple behavior types.

Encapsulation
44. What is Encapsulation?

 Binding data and methods together.


 Keeping variables private and exposing them via getters/setters.

🚫 Exception Handling
45. What is an exception?

 An error that occurs during runtime.

46. Types of exceptions:

 Checked (must be handled): e.g., IOException


 Unchecked (runtime): e.g., NullPointerException

47. Common exception keywords:


try {
// risky code
} catch (Exception e) {
// handle
} finally {
// always runs
}

48. Can we have multiple catch blocks?

 ✅ Yes, to handle different exception types.


49. What is finally block?

 Always runs — even if an exception is not thrown or caught.

📚 Java Class Basics


50. What is a constructor?

 Special method with same name as class.


 Runs when an object is created.

51. Types of constructors:

 Default (no arguments)


 Parameterized (with arguments)
 Copy constructor (not built-in, can be written manually)

52. Can a constructor be private?

 ✅ Yes, used in Singleton Pattern or factory methods.

53. this vs super:


Keywo
Refers to…
rd

Current class
this
instance

super Parent class object

📂 Miscellaneous
54. What is the Java Virtual Machine (JVM)?

 It runs Java bytecode on your computer.

55. What is the Java Development Kit (JDK)?

 Includes compiler, tools, libraries to write Java programs.


56. Difference between JDK, JRE, JVM?
Ter
Stands for Purpose
m

JVM Java Virtual Machine Runs Java code

Java Runtime
JRE JVM + libraries
Environment

JRE + compiler +
JDK Java Development Kit
tools

57. What is main() method signature?


public static void main(String[] args)

 public – can be called from anywhere


 static – runs without object
 void – no return
 String[] args – command-line args

58. What is the use of static keyword?

 Makes a method or variable belong to the class, not object.

59. What is the final keyword used for?

 Prevents:
o Variable from being reassigned
o Method from being overridden
o Class from being extended

60. What is a package in Java?

 A folder-like structure used to group related classes.

Would you like this in:


 📄 PDF for printing?
 📋 One-liner notes?
 🧪 Quiz format?

Let me know — I can send in whichever format you prefer next!

Absolutely! Let’s continue with more Java interview and concept questions to strengthen
your fundamentals:

🧱 Object Class & Basics


61. What is the Object class in Java?

 It’s the superclass of all classes in Java.


 Every class directly or indirectly extends Object.

62. Common methods in Object class:

 toString(): Converts object to string


 equals(): Checks equality between objects
 hashCode(): Returns hash code
 clone(): Creates a copy (if allowed)
 finalize(): Called before garbage collection

63. Difference between == and .equals()

Operato
Compares Used for
r

Reference (memory Are they same


==
address) object?

Are they logically


.equals() Content/data
equal?

🧹 Garbage Collection
64. What is garbage collection?

 Automatic memory management in Java.


 Removes unused objects from memory.
65. Can you force garbage collection?

 You can request using System.gc(), but JVM may ignore it.

🔁 Control Flow
66. Types of loops in Java:

 for
 while
 do-while
 Enhanced for-each (for arrays and collections)

67. Difference between break and continue:

 break: exits loop completely


 continue: skips current iteration and continues loop

💾 Arrays & Strings


68. How to declare an array?
int[] nums = new int[5];

69. String is immutable — what does it mean?

 Once created, a String’s value cannot be changed.


 Any change creates a new object in memory.

70. String vs StringBuilder vs StringBuffer:


Mutab
Class Thread-safe Performance
le

✅ Slow if many
String ❌
(immutable) changes

StringBuilder ✅ ❌ Fast

Slower than
StringBuffer ✅ ✅
Builder
📦 Collections Framework
71. What is a Collection?

 A Java framework to store and manipulate groups of objects


like lists, sets, maps.

72. Common interfaces:

 List → ordered, allows duplicates (ArrayList, LinkedList)


 Set → no duplicates (HashSet, TreeSet)
 Map → key-value pairs (HashMap, TreeMap)

73. ArrayList vs LinkedList


Feature ArrayList LinkedList

Storage Array-based Node-based

Access Fast
Slow
speed (indexing)

Insert/ Faster at
Slower
delete ends

74. HashSet vs TreeSet


Featu
HashSet TreeSet
re

Unordere
Order Sorted
d

Fast Slower (O(log


Speed
(O(1)) n))

🔒 Multithreading & Concurrency (Intro)


75. What is multithreading?

 Running multiple threads (small tasks) at the same time in one


program.
76. How to create a thread?

 Extend Thread class OR implement Runnable interface

77. Difference between start() and run() methods?

 start() → begins a new thread and calls run()


 run() → just runs like a normal method if called directly

78. Synchronized keyword:

 Prevents multiple threads from accessing critical section at the


same time.

🔗 Java Keywords & Miscellaneous


79. Transient keyword:

 Used in serialization — prevents variable from being saved

80. Volatile keyword:

 Tells JVM that a variable may be changed by multiple threads

81. What is Serialization?

 Converting an object into byte stream to save or send over


network.

82. What is instanceof operator?

 Checks if an object is an instance of a specific class or subclass

if (obj instanceof String) { ... }


83. What is autoboxing and unboxing?

 Autoboxing: Primitive → Object

int a = 10; Integer obj = a; // autoboxing

 Unboxing: Object → Primitive

Integer obj = 20; int b = obj; // unboxing

84. What is the enum keyword?

 Used to define fixed set of constants

enum Day { MONDAY, TUESDAY, ... }

85. What is the ternary operator?

 Shortcut for if-else:

int a = (x > 10) ? 1 : 0;

Java 8+ Concepts (Briefly)


86. Lambda Expression:

 Short way to write method using ->

(a, b) -> a + b;

87. Stream API:

 Allows operations like filter(), map(), collect() on data (like


arrays/lists)

88. Functional Interface:

 Interface with only one abstract method


 e.g., Runnable, Comparator, Function
✅ Final Recap (Important 5 for Viva):
Concept Must Know

Inheritance, Polymorphism, Encapsulation,


OOP Pillars
Abstraction

Access Modifiers private, default, protected, public

Data Types primitive vs reference

Class vs Object Object is instance of a class

Inheritance vs
One for extending, one for behavior
Interface

Would you like:

 📄 Full PDF Notes


 📊 MCQ quiz
 🎓 Mock Viva Q&A
 💻 Java code examples?

Let me know what helps you prepare better!

You might also like