Tcs Ipa Java
Tcs Ipa Java
What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now Oracle) in
1995.
This means that a Java program is compiled only once and can run on any operating system (Windows,
Linux, macOS) that has a Java Virtual Machine (JVM).
Example:
After compilation, the generated .class file can run on Windows, Linux, or macOS without changing the
code.
Features of Java
1. Object-Oriented
Everything in Java revolves around objects and classes.
• Encapsulation
• Inheritance
• Polymorphism
1
• Abstraction
TCS MCQ
Answer: C
2. Platform Independent
This is the most frequently asked Java MCQ.
The JVM of each operating system converts Bytecode into machine code.
Flow:
Compiler (javac)
Bytecode (.class)
JVM
Machine Code
2
↓
Output
Example:
3. Secure
Java is considered secure because:
• No pointers • Bytecode verifier checks code before execution • Automatic memory management • Class
Loader prevents unauthorized classes
4. Robust
Robust means reliable.
5. Portable
Java programs can be moved from one system to another without modification.
3
6. Distributed
Java supports distributed applications through networking packages like:
[Link]
Example:
Client-Server Applications
7. Multithreaded
Java can execute multiple tasks simultaneously.
Example:
8. Dynamic
Java loads classes during runtime.
9. Architecture Neutral
Java Bytecode does not depend on CPU architecture.
It works on:
4
JIT converts Bytecode into Machine Code during execution.
[Link]
Compile:
javac [Link]
Compiler creates:
[Link]
Run:
java Hello
Execution Process:
Step 1
Write
[Link]
Step 2
Compile
javac [Link]
Step 3
Compiler generates
[Link]
5
↓
Step 4
Step 5
Step 6
Output appears
Responsibilities:
• Loads class files • Executes Bytecode • Performs Garbage Collection • Manages memory
6
Purpose:
Purpose:
Relationship
JDK
contains
JRE
contains
JVM
Memory Trick:
Develop → JDK
Run → JRE
7
Execute → JVM
Important Commands
Compile
javac [Link]
Run
java Hello
File Created
[Link]
Java is developed by
Answer: B
MCQ 2
Answer: B
Explanation: The compiler generates Bytecode, which can run on any platform with a JVM.
8
MCQ 3
Java follows
Answer: B
MCQ 4
Answer: B
MCQ 5
Answer: B
MCQ 6
Answer: B
MCQ 7
Answer: B
9
MCQ 8
Answer: B
Explanation: The JVM executes Bytecode, and the JIT compiler inside the JVM converts frequently used
Bytecode into Machine Code.
MCQ 9
Answer: C
MCQ 10
Answer: D
Explanation: JDK includes the JRE, so both can run Java applications.
MCQ 11
Answer: C
MCQ 12
10
A) JDK contains JVM. B) JVM contains JDK. C) JRE contains JDK. D) Compiler is inside JVM.
Answer: A
MCQ 13
A) Write Once Run Anywhere B) Write Everywhere Run Once C) Compile Everywhere D) Run Anywhere
Compile Anywhere
Answer: A
MCQ 14
Answer: C
MCQ 15
Answer: C
11
✔ JRE = JVM + Libraries.
✔ JIT Compiler improves performance by converting Bytecode to Machine Code during execution.
12
CORE JAVA FOR TCS IPA – Chapter 2: Variables
(Detailed Notes + MCQs)
What is a Variable?
A variable is a named memory location used to store data. Every variable has:
Example:
Here:
1. Local Variable
2. Instance Variable
3. Static Variable (Class Variable)
Understanding the difference between these is one of the most frequently tested concepts in TCS MCQs.
1. Local Variable
A local variable is declared inside a method, constructor, or block.
Example:
1
public class Test {
public static void main(String[] args) {
int x = 10;
[Link](x);
}
}
Here,
x is a local variable.
Characteristics
✔ No default value
Example:
int x;
[Link](x);
Output:
2
Compile-Time Error
variable x might not have been initialized
Reason:
2. Instance Variable
An instance variable is declared inside a class but outside all methods, constructors, and blocks.
Example:
Example:
Memory:
s1 → marks = 80
s2 → marks = 80
[Link] = 90;
3
Now:
s1 → 90
s2 → 80
Characteristics
✔ Outside methods
Example:
Now,
Example:
4
Student s1 = new Student();
Memory:
college
KIT
s1
s2
If
[Link] = "MIT";
Then
MIT
Characteristics
✔ Saves memory
5
Comparison Table
Feature Local Variable Instance Variable Static Variable
Lifetime Until method ends Until object is destroyed Until program ends/class unloaded
Shared No No Yes
✔ Instance Variables
✔ Static Variables
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0
boolean false
Object null
6
Example
int x;
boolean flag;
char ch;
String name;
[Link](t.x);
[Link]([Link]);
[Link]((int)[Link]);
[Link]([Link]);
Output:
false
null
Explanation:
7
Scope of Variables
Local Variable
Accessible only inside its method.
Instance Variable
Accessible through object.
Static Variable
Accessible using class name.
Example:
[Link]
Memory Allocation
Local Variable
Stack Memory
Instance Variable
Heap Memory
Static Variable
8
Frequently Asked TCS MCQs
MCQ 1
A) Instance Variable
B) Static Variable
C) Local Variable
D) Object Variable
Answer: C
MCQ 2
A) Local Variable
B) Instance Variable
C) Static Variable
D) Constructor
Answer: C
MCQ 3
A) Inside method
B) Outside class
C) Inside package
D) Inside JVM
9
Answer: A
MCQ 4
A) null
B) 0
C) 0.0
D) Undefined
Answer: B
MCQ 5
A) true
B) false
C) null
D) 0
Answer: B
MCQ 6
A) '0'
B) '\u0000'
C) null
D) Empty String
10
Answer: B
MCQ 7
A) 0
B) false
C) null
D) '\u0000'
Answer: C
MCQ 8
A) Heap
B) Stack
C) Method Area
D) JVM Cache
Answer: B
MCQ 9
A) Stack
B) Heap
C) Registers
D) Cache
11
Answer: B
MCQ 10
A) Stack
B) Heap
D) CPU Registers
Answer: C
int x;
[Link](t.x);
Options:
A) Compile Error
B) null
C) 0
D) Garbage Value
Answer: C
12
Explanation: x is an instance variable, so it gets the default value 0 .
int x;
[Link](x);
Options:
A) 0
B) null
C) Compile-Time Error
D) Garbage Value
Answer: C
Memory Trick
LIS Rule
13
One-Minute Revision
✔ Java has 3 variable types: Local, Instance, Static.
✔ Local variables:
✔ Instance variables:
✔ Static variables:
✔ Remember:
14
CORE JAVA FOR TCS IPA – Chapter 3: Data Types
(Detailed Notes + MCQs)
Example:
Here:
Without a data type, Java cannot determine how to store the value in memory.
Data Types
1
byte
short
int
long
float
double
char
boolean
Examples:
• String
• Arrays
• Classes
• Interfaces
• Objects
• Enums
Example:
Here,
2
Primitive Data Types in Detail
1. byte
Size:
1 Byte = 8 bits
Range:
-128 to 127
Example
byte a = 100;
Use:
2. short
Size
2 Bytes
Range
-32,768 to 32,767
Example
3
3. int
Most commonly used integer type.
Size
4 Bytes
Range
-2^31 to (2^31)-1
Example
If you write
int x = 10;
4. long
Size
8 Bytes
Example
4
L
Without L
long x = 5000000000;
Compile Error
Reason:
Always use
5000000000L
5. float
Stores decimal numbers.
Size
4 Bytes
Example
Notice
must be written.
Without f
5
float x = 10.5;
Compile Error
Reason:
6. double
Most commonly used decimal datatype.
Size
8 Bytes
Example
double pi = 3.14159;
No suffix required.
float vs double
float double
4 Bytes 8 Bytes
Example
float a = 12.5f;
double b = 12.5;
6
7. char
Stores a single Unicode character.
Size
2 Bytes
Example
Single Quotes
Wrong
char c = "A";
Compile Error
Correct
char c = 'A';
char ch = 65;
[Link](ch);
Output
Because
7
ASCII value of A = 65
8. boolean
Stores only two values.
true
false
Size
JVM dependent
Example
Cannot store
Yes
No
Wrong
boolean b = 1;
Compile Error
8
Memory Table (Must Memorize)
Data Type Size Default Value Example
Example
double d = 10.5;
Works perfectly.
Now
float f = 10.5;
Compile Error
Reason:
Correct
9
float f = 10.5f;
Remember
Decimal → double
Type Conversion
Widening (Implicit Conversion)
Smaller datatype → Bigger datatype
No data loss.
byte
short
int
long
float
double
Example
10
int x = 100;
double d = x;
Output
100.0
Example
double d = 10.8;
int x = (int)d;
Output
10
Type Casting
Example
int x = 65;
char c = (char)x;
[Link](c);
Output
11
A
A) 6
B) 7
C) 8
D) 9
Answer: C
MCQ 2
Which is the largest integer datatype?
A) int
B) short
C) long
D) byte
Answer: C
Explanation: long is an 8-byte integer type and can store larger integer values than int .
MCQ 3
Default decimal literal is
12
A) float
B) double
C) long
D) int
Answer: B
Explanation: Any decimal value (e.g., 10.5 ) is treated as a double unless you add the f suffix.
MCQ 4
Which datatype stores a single character?
A) String
B) char
C) Character[]
D) byte
Answer: B
MCQ 5
Size of char?
A) 1 Byte
B) 2 Bytes
C) 4 Bytes
D) JVM dependent
Answer: B
13
MCQ 6
Which datatype stores only true or false?
A) bool
B) bit
C) boolean
D) logical
Answer: C
MCQ 7
Which statement is correct?
float f = 10.5;
A) Correct
B) Runtime Error
C) Compile-Time Error
D) Prints 10.5
Answer: C
float f = 10.5f;
MCQ 8
Output?
14
char ch = 65;
[Link](ch);
A) 65
B) A
C) Compile Error
D) null
Answer: B
MCQ 9
Output?
double d = 15.9;
int x = (int)d;
[Link](x);
A) 15
B) 16
C) 15.9
D) Compile Error
Answer: A
MCQ 10
Which is NOT a primitive datatype?
15
A) int
B) String
C) char
D) boolean
Answer: B
MCQ 11
What is the size of double ?
A) 2 Bytes
B) 4 Bytes
C) 8 Bytes
D) JVM dependent
Answer: C
MCQ 12
Output?
long x = 100;
[Link](x);
A) 100
B) 100L
C) Compile Error
D) Runtime Error
16
Answer: A
Explanation: The L suffix is optional when the value fits within the int range.
MCQ 13
Which conversion is automatic?
A) double → int
B) long → byte
C) int → double
D) float → short
Answer: C
Explanation: Converting from a smaller type to a larger type is called widening, and Java performs it
automatically.
MCQ 14
What is the default value of a boolean instance variable?
A) true
B) false
C) null
D) 0
Answer: B
MCQ 15
Which of the following is the correct declaration?
A)
17
char c = "A";
B)
char c = 'A';
C)
char c = A;
D)
char c = "65";
Answer: B
float f = 5.5;
✔ Correct
float f = 5.5f;
❌ Wrong
char c = "A";
✔ Correct
18
char c = 'A';
❌ Wrong
boolean b = 1;
✔ Correct
boolean b = true;
❌ Wrong
long l = 5000000000;
✔ Correct
long l = 5000000000L;
Memory Trick
Remember the sizes using this sequence:
byte → 1
short → 2
int → 4
long → 8
float → 4
double → 8
char → 2
19
boolean → JVM Dependent
Or simply remember:
• 1 Byte → byte
• 2 Bytes → short, char
• 4 Bytes → int, float
• 8 Bytes → long, double
One-Minute Revision
✔ Java has 8 primitive data types.
✔ These concepts are among the most frequently tested in TCS IPA Java MCQs.
20
CORE JAVA FOR TCS IPA – Chapter 4: Operators
(Detailed Notes + MCQs)
What is an Operator?
An operator is a special symbol used to perform operations on variables or values.
Example:
int a = 10;
int b = 20;
int c = a + b;
Here,
+ is an operator.
1. Arithmetic Operators
2. Unary Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Shift Operators
7. Assignment Operators
8. Ternary Operator
9. instanceof Operator
1
1. Arithmetic Operators
Used for mathematical calculations.
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
Example
int a = 15;
int b = 4;
[Link](a+b);
[Link](a-b);
[Link](a*b);
[Link](a/b);
[Link](a%b);
Output
19
11
60
Explanation:
15%4 = 3 (Remainder)
2
Integer Division
Example
[Link](10/3);
Output
Now,
[Link](10.0/3);
Output
3.333333...
Modulus Operator %
Returns remainder.
Example
17 % 5
Output
3
Unary Operators
Unary means working on only one operand.
++
--
!
Pre Increment
++a
Increment first
Use later
Example
int a=5;
[Link](++a);
Output
4
Post Increment
a++
Use first
Increment later
Example
int a=5;
[Link](a++);
Output
Now
a becomes
Decrement (--)
Same logic.
--a
Decrease first.
a--
Use first.
Decrease later.
5
TCS Output Questions
Question 1
int x=5;
[Link](x++);
[Link](x);
Output
Question 2
int x=5;
[Link](++x);
Output
Question 3
int x=5;
[Link](x++ + ++x);
Step
5 + 7
6
Output
12
Relational Operators
Used to compare two values.
Always return
true
or
false
Operator Meaning
== Equal
!= Not Equal
Example
int a=10;
[Link](a>5);
Output
true
7
== vs =
One of the most asked MCQs.
Assignment Operator
==
Comparison Operator
Example
int a=10;
Assignment
Example
a==10
Comparison
Returns
true
Logical Operators
Used with boolean values.
Operator Meaning
! Logical NOT
8
Logical AND (&&)
Returns true only if both conditions are true.
Truth Table
A B Result
T T T
T F F
F T F
F F F
Example
int age=20;
Output
true
Logical OR (||)
Returns true if any one condition is true.
Truth Table
A B Result
T T T
T F T
F T T
F F F
9
Logical NOT (!)
Reverses boolean.
Example
boolean b=true;
[Link](!b);
Output
false
Example
int a=5;
[Link](a);
Output
10
5
Reason:
Example
int a=5;
[Link](a);
Output
Reason:
Comparison
&& &
11
&& &
Bitwise Operators
Operate on binary numbers.
Operator Meaning
& AND
| OR
^ XOR
~ Complement
Example
5 = 0101
3 = 0011
Bitwise AND
0101
0011
-----
0001
Output
12
XOR (^)
Rule
Same → 0
Different → 1
Example
5 ^ 3
Complement (~)
Flips every bit.
Example
[Link](~5);
Output
-6
Shift Operators
Operator Meaning
13
Operator Meaning
Example
5<<1
Binary
0101
1010
Output
10
Example
8>>1
Output
Memory Trick
Left Shift
Right Shift
14
2
Assignment Operators
Operator Meaning
= Assignment
Example
int x=10;
x+=5;
Equivalent to
x=x+5;
Output
15
Ternary Operator
Syntax
15
Example
int a=10;
String result=(a>5)?"Yes":"No";
Output
Yes
instanceof Operator
Checks whether an object belongs to a particular class.
Example
String s="Java";
Output
true
()
Unary (++ -- !)
* / %
+ -
<< >>
16
< <= > >=
== !=
&
&&
||
?:
Remember:
BODMAS does not fully apply in Java. Java follows operator precedence rules.
A) +
B) *
C) ==
D) &&
Answer: B
MCQ 2
17
A) Both are same
C) & is faster
D) None
Answer: B
MCQ 3
Output
int a=5;
[Link](a++);
A) 5
B) 6
C) Compile Error
D) Runtime Error
Answer: A
MCQ 4
Output
int a=5;
[Link](++a);
A) 5
B) 6
C) 7
18
D) Error
Answer: B
MCQ 5
Output
[Link](10/3);
A) 3
B) 3.33
C) 3.0
D) Compile Error
Answer: A
MCQ 6
Output
[Link](10%3);
A) 0
B) 1
C) 3
D) 10
Answer: B
MCQ 7
Output
19
[Link](5<<1);
A) 5
B) 10
C) 2
D) 20
Answer: B
MCQ 8
Output
[Link](8>>1);
A) 4
B) 8
C) 16
D) 2
Answer: A
MCQ 9
Output
int x=10;
x+=5;
[Link](x);
A) 10
20
B) 15
C) 5
D) Error
Answer: B
MCQ 10
A) typeof
B) instanceof
C) is
D) classof
Answer: B
MCQ 11
Output
boolean b=true;
[Link](!b);
A) true
B) false
C) 1
D) Error
Answer: B
21
MCQ 12
Output
[Link](~5);
A) 5
B) -5
C) -6
D) 6
Answer: C
Explanation: ~ flips all bits. For 5 , the result is -6 in Java's two's complement representation.
if(a=10)
✔ Correct
if(a==10)
❌ Wrong
✔ Prefer
&&
22
Memory Tricks
Arithmetic
+ - * / %
Relational
Logical
&& || !
Bitwise
& | ^ ~
Shift
Assignment
= += -= *= /= %=
One-Minute Revision
✔ Arithmetic operators perform mathematical operations.
✔ = is assignment, == is comparison.
23
✔ && uses short-circuit evaluation.
✔ 10 / 3 = 3 (integer division).
✔ 10 % 3 = 1 (remainder).
✔ 5 << 1 = 10 .
✔ 8 >> 1 = 4 .
These concepts are among the highest-frequency MCQ topics in TCS IPA Core Java.
24
CORE JAVA FOR TCS IPA – Chapter 5: Control
Statements (Detailed Notes + MCQs)
Control Statements
1
├── continue
└── return
1. if Statement
Used when you want to execute code only if a condition is true.
Syntax
if(condition){
// statements
}
Example
Output
Eligible to Vote
2. if-else Statement
Used when there are two possible outcomes.
Syntax
if(condition){
// True block
}
else{
2
// False block
}
Example
Output
Fail
3. else-if Ladder
Used when multiple conditions need to be checked.
Example
Output
3
Grade B
4. Nested if
An if statement inside another if statement.
Example
Output
Eligible
5. switch Statement
Used when multiple cases depend on the same expression.
Syntax
switch(expression){
case value1:
statements;
break;
case value2:
statements;
break;
4
default:
statements;
}
Example
int day = 3;
switch(day){
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Invalid");
}
Output
Wednesday
break in switch
break exits the switch after executing a matching case.
Example
int n = 2;
5
switch(n){
case 1:
[Link]("One");
case 2:
[Link]("Two");
case 3:
[Link]("Three");
}
Output
Two
Three
default Case
Executed when no case matches.
Example
int n = 10;
switch(n){
case 1:
[Link]("One");
break;
default:
[Link]("Invalid");
}
Output
Invalid
6
Datatypes Allowed in switch
Allowed
byte
short
char
int
enum
Not Allowed
long
float
double
boolean
Memory Trick
Allowed
B S C I S E
7
Example
switch(day){
case "Monday":
[Link]("Start");
break;
case "Sunday":
[Link]("Holiday");
}
Output
Start
6. for Loop
Used when the number of iterations is known.
Syntax
Example
for(int i=1;i<=5;i++){
[Link](i);
}
Output
1
2
3
8
4
5
Execution Order
Initialization
Condition
Statements
Update
Condition
for(;;){
[Link]("Java");
}
Runs forever.
7. while Loop
Checks the condition first.
Syntax
9
while(condition){
statements;
}
Example
int i=1;
while(i<=5){
[Link](i);
i++;
}
Output
1
2
3
4
5
while(true){
Runs forever.
8. do-while Loop
Difference:
Syntax
10
do{
statements;
}while(condition);
Example
int i=10;
do{
[Link](i);
}while(i<5);
Output
10
Condition is false,
Difference
while do-while
break Statement
Immediately exits the loop.
Example
11
for(int i=1;i<=5;i++){
if(i==3)
break;
[Link](i);
}
Output
1
2
continue Statement
Skips the current iteration.
Example
for(int i=1;i<=5;i++){
if(i==3)
continue;
[Link](i);
}
Output
1
2
4
5
return Statement
Terminates the current method immediately.
12
Example
[Link]("A");
return;
// [Link]("B"); // Unreachable
}
Execution continues after loop Continues next iteration Method ends immediately
A) for
B) if
C) while
D) break
Answer: B
MCQ 2
Which loop executes at least once?
13
A) for
B) while
C) do-while
D) none
Answer: C
MCQ 3
Which statement exits a loop immediately?
A) continue
B) return
C) break
D) exit
Answer: C
MCQ 4
Which statement skips the current iteration?
A) break
B) continue
C) return
D) goto
Answer: B
14
MCQ 5
Which statement exits the current method?
A) continue
B) break
C) return
D) exit
Answer: C
MCQ 6
Can switch use String ?
A) No
C) Only Java 8
D) Only Java 11
Answer: B
MCQ 7
Which datatype is NOT allowed in switch ?
A) char
B) int
C) long
D) String
Answer: C
15
MCQ 8
Output
int i=1;
while(i<=3){
[Link](i);
i++;
}
A) 123
B) 321
C) Infinite
D) Error
Answer: A
MCQ 9
Output
for(int i=1;i<=5;i++){
if(i==3)
break;
[Link](i);
}
A) 12345
B) 12
C) 1245
16
D) Error
Answer: B
MCQ 10
Output
for(int i=1;i<=5;i++){
if(i==3)
continue;
[Link](i);
}
A) 12345
B) 12
C) 1245
D) 345
Answer: C
MCQ 11
Output
int i=10;
do{
[Link](i);
}while(i<5);
A) Nothing
17
B) 10
C) Infinite
D) Compile Error
Answer: B
MCQ 12
What happens if break is omitted in a switch case?
A) Compile Error
B) Runtime Error
D) Program terminates
Answer: C
MCQ 13
Which of the following creates an infinite loop?
A)
for(;;){}
B)
while(true){}
C)
do{}while(true);
18
D) All of the above
Answer: D
MCQ 14
Which statement about switch is TRUE?
Answer: B
MCQ 15
Execution order in a for loop is:
Answer: B
switch(5.5)
✔ Correct
19
switch(5)
❌ Wrong
switch(true)
❌ Wrong
Memory Tricks
Allowed in switch
B S C I S E
Byte
Short
Char
Int
String
Enum
Not Allowed
long
float
double
20
boolean
One-Minute Revision
✔ if executes code only when the condition is true.
TCS Tip: The most frequently asked questions from this chapter involve:
21
CORE JAVA FOR TCS IPA – Chapter 6: Arrays
(Detailed Notes + MCQs)
What is an Array?
An array is a collection of elements of the same data type stored in contiguous memory locations.
Instead of creating multiple variables, we can store many values in a single array.
Example:
Instead of writing
int m1 = 80;
int m2 = 85;
int m3 = 90;
int m4 = 95;
int m5 = 88;
We can write
Advantages:
Characteristics of Arrays
✔ Fixed size.
✔ Indexed from 0.
1
✔ Arrays are Objects in Java.
Array Declaration
There are two valid ways.
Method 1
int arr[];
Method 2
int[] arr;
TCS MCQ:
A)
int arr[];
B)
int[] arr;
C) Both
D) None
Answer:
2
Array Creation
Syntax
Example
Memory
Index
0 1 2 3 4
0 0 0 0 0
Array Initialization
Method 1
Method 2
Method 3
3
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
Accessing Elements
Index starts from
Example
[Link](arr[0]);
[Link](arr[2]);
Output
10
30
Indexing
Index
0 1 2 3
4
10 20 30 40
Remember
First index = 0
Array Length
Length is a property.
Syntax
[Link]
Not
[Link]()
Example
int[] arr={10,20,30,40};
[Link]([Link]);
Output
Strings use
5
length()
Collections use
size()
Memory Trick
Array
length
String
length()
ArrayList
size()
byte 0
short 0
int 0
long 0L
float 0.0f
6
Data Type Default Value
double 0.0
char '\u0000'
boolean false
Object null
Example
[Link](arr[0]);
[Link](arr[1]);
Output
Array Traversal
Using for loop
int[] arr={10,20,30};
for(int i=0;i<[Link];i++){
[Link](arr[i]);
}
Output
10
20
7
30
Example
int[] arr={10,20,30};
for(int x:arr){
[Link](x);
}
Output
10
20
30
Advantages
✔ Simple
✔ No index required
Disadvantage
8
Multidimensional Arrays
Example
int[][] arr={
{1,2},
{3,4}
};
Memory
1 2
3 4
Access
[Link](arr[1][0]);
Output
ArrayIndexOutOfBoundsException
Most common runtime exception.
Example
int[] arr={10,20,30};
[Link](arr[5]);
Output
9
ArrayIndexOutOfBoundsException
Reason
sort()
import [Link];
int[] arr={30,10,20};
[Link](arr);
[Link]([Link](arr));
Output
[10,20,30]
binarySearch()
int[] arr={10,20,30,40};
[Link]([Link](arr,30));
Output
10
2
fill()
[Link](arr,7);
Output
7 7 7 7 7
equals()
[Link](a,b)
Example
Output
11
true
Reason
B) Classes
C) Objects
D) Interfaces
Answer: C
MCQ 2
Array indexing starts from
A) 1
B) 0
C) -1
D) Depends on JVM
Answer: B
MCQ 3
Length of array is obtained using
12
A) length()
B) size()
C) length
D) getLength()
Answer: C
MCQ 4
Which receives default values?
A) Local Variables
B) Arrays
C) Parameters
D) Methods
Answer: B
MCQ 5
Default value of an int array element?
A) null
B) 0
C) Garbage Value
D) Undefined
Answer: B
MCQ 6
Output
13
int[] arr=new int[3];
[Link](arr[2]);
A) null
B) 0
C) Compile Error
D) Runtime Error
Answer: B
MCQ 7
Output
int[] arr={10,20,30};
[Link]([Link]);
A) 2
B) 3
C) 4
D) Error
Answer: B
MCQ 8
Output
int[] arr={10,20};
[Link](arr[2]);
14
A) null
B) 0
C) Compile Error
D) ArrayIndexOutOfBoundsException
Answer: D
MCQ 9
Which package contains Arrays class?
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: C
MCQ 10
Which method sorts an array?
A) [Link]()
B) [Link]()
C) [Link]()
D) sortArray()
Answer: B
MCQ 11
Output
15
int[] arr={10,20,30};
for(int x:arr){
[Link](x);
}
A) 102030
B) 123
C) Error
D) Infinite
Answer: A
MCQ 12
Which loop is specially designed for arrays?
A) while
B) for
D) do-while
Answer: C
MCQ 13
Which statement is TRUE?
16
Answer: B
MCQ 14
Last valid index of an array of size 10?
A) 10
B) 9
C) 8
D) 11
Answer: B
Explanation:
MCQ 15
Which method converts an array into a readable String?
A) [Link]()
B) [Link]()
C) [Link]()
D) [Link]()
Answer: B
[Link]()
17
✔ Correct
[Link]
❌ Wrong
arr[5]
When size is 5.
Valid indices
0–4
❌ Wrong
[Link]()
on an unsorted array.
Memory Tricks
Remember
Array
length
String
18
length()
ArrayList
size()
Remember
First Index = 0
One-Minute Revision
✔ Arrays store multiple values of the same data type.
• sort()
• binarySearch()
• fill()
• equals()
19
• toString()
TCS Tip: Questions on [Link] vs length() , default values, array indexing, exceptions, and output-
based programs appear very frequently in TCS IPA Java assessments.
20
CORE JAVA FOR TCS IPA – Chapter 7: String
(Detailed Notes + MCQs)
What is a String?
A String is a sequence of characters.
Example:
Here,
H a r s h a d
is a String.
The String class belongs to the [Link] package, which is automatically imported.
• Names
• Passwords
• URLs
• Email IDs
• JSON
• File Paths
1
String is Immutable
The most important concept.
Immutable means:
Example
String s = "Java";
[Link](" Programming");
[Link](s);
Output
Java
Why?
Because
concat()
Correct
String s = "Java";
s = [Link](" Programming");
[Link](s);
Output
2
Java Programming
Memory
Before
Java
After concat()
Java
Programming
(New Object)
After assignment
Java Programming
• Security
• Thread Safety
• String Pool Optimization
• HashMap Keys
• Performance
3
Ways to Create Strings
Method 1 (String Literal)
String s = "Hello";
Stored inside
Creates an object in
• Heap Memory
and also uses the String Constant Pool if the literal is not already present.
Example
String s1 = "Java";
String s2 = "Java";
String s3 = "Java";
Memory
4
SCP
Java
s1
s2
s3
Objects Created
Example
String s1 = "abc";
String s2 = "abc";
Objects created?
One
Reason
Now
Objects created?
5
Two
One
String Pool
One
Heap
Memory
Heap
abc
Reference
SCP
abc
== vs equals()
Very Important
==
Checks reference (memory address).
Example
6
String s1 = "Java";
String s2 = "Java";
[Link](s1 == s2);
Output
true
Now
[Link](s1 == s2);
Output
false
Different objects.
equals()
Checks content.
Example
[Link]([Link](s2));
Output
7
true
Comparison Table
== equals()
Returns true if both refer to same object Returns true if values are equal
Operator Method
Memory Trick
==
Address
equals()
Content
String s = "Harshad";
[Link]([Link]());
Output
8
7
2. charAt(index)
Returns character at given index.
String s = "Java";
[Link]([Link](2));
Output
3. substring()
Extracts part of String.
String s = "Programming";
[Link]([Link](3));
Output
gramming
Another example
[Link]([Link](3,7));
Output
gram
9
4. indexOf()
Returns first occurrence.
String s = "banana";
[Link]([Link]('a'));
Output
5. lastIndexOf()
Returns last occurrence.
[Link]("banana".lastIndexOf('a'));
Output
6. replace()
String s = "Java";
[Link]([Link]('a','o'));
Output
Jovo
10
7. trim()
Removes leading and trailing spaces.
[Link]([Link]());
Output
Java
8. contains()
Checks whether substring exists.
[Link]("Programming".contains("gram"));
Output
true
9. startsWith()
[Link]("Programming".startsWith("Pro"));
Output
true
10. endsWith()
[Link]("Programming".endsWith("ing"));
11
Output
true
11. equalsIgnoreCase()
[Link]("JAVA".equalsIgnoreCase("java"));
Output
true
12. split()
Output
Java
Python
13. toUpperCase()
"java".toUpperCase()
Output
JAVA
12
14. toLowerCase()
"JAVA".toLowerCase()
Output
java
15. isEmpty()
"".isEmpty()
Output
true
16. concat()
"Java".concat(" Programming")
Output
Java Programming
Remember
concat() does not modify the original String because Strings are immutable.
StringBuffer
Mutable.
Thread Safe.
13
Slower.
Example
[Link](" Programming");
[Link](sb);
Output
Java Programming
StringBuilder
Mutable.
Faster.
Example
[Link](" Programming");
[Link](sb);
Output
Java Programming
14
String vs StringBuffer vs StringBuilder
Feature String StringBuffer StringBuilder
Memory Trick
String
Immutable
Safe
StringBuffer
Mutable
Thread Safe
Slow
StringBuilder
Mutable
15
↓
Fastest
String is
A) Mutable
B) Immutable
C) Interface
D) Abstract
Answer: B
MCQ 2
Output
String s="Java";
[Link](" Programming");
[Link](s);
A) Java Programming
B) Java
C) Programming
D) Error
Answer: B
16
MCQ 3
String s1="abc";
String s2="abc";
Objects created?
A) 1
B) 2
C) 3
D) 4
Answer: A
MCQ 4
new String("abc")
Objects created?
A) 1
B) 2
C) 3
D) Depends
Answer: B
MCQ 5
17
C) equals() compares references
D) No difference
Answer: B
MCQ 6
A) String
B) StringBuffer
C) StringBuilder
D) Character
Answer: C
MCQ 7
A) StringBuilder
B) StringBuffer
C) String
D) Character
Answer: B
MCQ 8
Output
[Link]("Java".length());
A) 3
18
B) 4
C) 5
D) Error
Answer: B
MCQ 9
Output
[Link]("Java".charAt(1));
A) J
B) a
C) v
D) Error
Answer: B
MCQ 10
Output
[Link]("Programming".substring(3,7));
A) gram
B) gramming
C) Prog
D) amm
Answer: A
19
MCQ 11
Output
[Link]("banana".indexOf('a'));
A) 0
B) 1
C) 3
D) 5
Answer: B
MCQ 12
Output
[Link]("JAVA".equalsIgnoreCase("java"));
A) true
B) false
C) Error
D) Null
Answer: A
MCQ 13
Output
[Link]("".isEmpty());
A) true
20
B) false
C) Error
D) null
Answer: A
MCQ 14
A) strip()
B) trim()
C) remove()
D) replace()
Answer: B
MCQ 15
A) split()
B) divide()
C) break()
D) parse()
Answer: A
21
if(s1==s2)
✔ Correct
if([Link](s2))
✔ Correct
[Link]()
Memory Tricks
Remember
Array
length
String
length()
ArrayList
22
size()
Remember
==
Reference
equals()
Content
Remember
String
Immutable
StringBuffer
Mutable
Thread Safe
StringBuilder
Mutable
23
Not Thread Safe
Fastest
One-Minute Revision
✔ Strings are immutable.
✔ new String("abc") creates two objects (one in the SCP, one in the Heap).
✔ == compares references.
✔ StringBuilder is mutable, not thread safe, and the fastest for string modifications.
TCS Tip: This is the highest-weightage Java topic. Be prepared for output-based questions, String Pool,
immutability, == vs equals() , String methods, and StringBuffer vs StringBuilder comparisons.
24
CORE JAVA FOR TCS IPA – Chapter 8: Object-
Oriented Programming (OOP) Concepts (Detailed
Notes + MCQs)
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
What is a Class?
A class is a blueprint or template used to create objects.
It defines:
Example:
Blueprint of a House
1
Similarly,
Class
Example of Class
class Student{
int rollNo;
String name;
void display(){
[Link](rollNo+" "+name);
Here,
Student is a Class.
It contains
• Variables
• Method
What is an Object?
An object is an instance of a class.
It is a real-world entity.
Example
2
Student s1 = new Student();
Here
Student
Class
s1
Object
Real-Life Example
Class
Car
Objects
BMW
Audi
Tesla
Class
Student
Objects
3
Harshad
Rahul
Amit
Creating an Object
Syntax
Example
Explanation
Student
Class
Reference Variable
new
Creates Object
Student()
4
Calls Constructor
Example
[Link] = 101;
[Link] = "Harshad";
[Link]();
Output
101 Harshad
Memory Representation
Heap Memory
------------------
Student Object
rollNo = 101
name = Harshad
------------------
Reference
5
↓
Constructor
A constructor is a special method used to initialize an object.
Example
class Student{
Student(){
[Link]("Constructor Called");
Output
Constructor Called
6
Characteristics of Constructor
✔ Same name as the class
✔ No return type
✔ Called automatically
✔ Can be overloaded
✔ Cannot be inherited
✔ Cannot be static
✔ Cannot be final
✔ Cannot be abstract
Correct
class Student{
Student(){
Wrong
class Student{
void student(){
7
}
Rule 2
Wrong
int Student(){
Wrong
void Student(){
Types of Constructors
Java has three commonly discussed constructor types.
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor (User-defined)
1. Default Constructor
A constructor with no parameters.
Example
8
class Student{
Student(){
[Link]("Default Constructor");
Output
Default Constructor
Example
class Student{
Internally,
Java creates
Student(){
Important:
If you write any constructor, the compiler does not create the default constructor.
9
2. Parameterized Constructor
A constructor with parameters.
Example
class Student{
int id;
Student(int x){
id = x;
Creating Object
[Link]([Link]);
Output
101
Advantages
We create it ourselves.
10
Example
class Student{
int id;
Student(int x){
id = x;
Student(Student s){
id = [Link];
Now,
Constructor Overloading
Java allows multiple constructors with different parameter lists.
Example
class Student{
Student(){
[Link]("Default");
11
Student(int x){
[Link](x);
Output
Default
100
Constructor vs Method
Constructor Method
❌ No
❌ No
12
❌ No
❌ No
✅ Yes
❌ No
A) Object
B) Blueprint
C) Variable
D) Method
Answer: B
MCQ 2
An object is
A) Blueprint
B) Instance of Class
13
C) Constructor
D) Variable
Answer: B
MCQ 3
Constructor is called
A) Manually
C) During compilation
D) By JVM shutdown
Answer: B
MCQ 4
Which statement about constructors is TRUE?
Answer: B
MCQ 5
Can a constructor have a return type?
A) Yes
B) No
14
C) Only void
D) Only int
Answer: B
MCQ 6
Can a constructor be static?
A) Yes
B) No
C) Only private
D) Only protected
Answer: B
MCQ 7
Which constructor is automatically provided by the compiler (if no constructor is written)?
A) Parameterized Constructor
B) Copy Constructor
C) Default Constructor
D) Static Constructor
Answer: C
MCQ 8
Java supports
A) Constructor Overloading
B) Constructor Overriding
15
C) Both
D) Neither
Answer: A
MCQ 9
Output
class Student{
Student(){
[Link]("Hello");
A) Hello
B) Error
C) Nothing
D) null
Answer: A
16
MCQ 10
Which keyword creates an object?
A) create
B) class
C) new
D) object
Answer: C
MCQ 11
Where are objects stored?
A) Stack Memory
B) Heap Memory
C) CPU Cache
D) Registers
Answer: B
MCQ 12
What is stored in Stack Memory?
A) Object
B) Reference Variable
C) Array Elements
D) Class Definition
Answer: B
17
MCQ 13
Which is NOT a characteristic of a constructor?
B) No return type
C) Can be static
D) Initializes objects
Answer: C
MCQ 14
Which statement is FALSE?
Answer: C
MCQ 15
Which of the following is correct?
A) Student is an object.
B) s is the class.
D) Student() is a method.
18
Answer: C
void Student(){
❌ Wrong
static Student(){
❌ Wrong
int Student(){
Memory Tricks
Remember
Class
19
Blueprint
Creates
Objects
Remember
Constructor
Same Name
No Return Type
Automatic Call
Remember
Default
No Parameters
Parameterized
Parameters
Copy
20
↓
Object as Parameter
One-Minute Revision
✔ A Class is a blueprint.
TCS Tip: This chapter is frequently tested through concept-based MCQs, output questions, and
constructor-related interview questions. Master this chapter before moving to this , super ,
Inheritance, and Polymorphism.
21
CORE JAVA FOR TCS IPA – Chapter 9: this Keyword
(Detailed Notes + MCQs)
Whenever an object calls a method or constructor, Java automatically passes the reference of that object
using this .
Example:
class Student{
void display(){
[Link](this);
[Link]();
Output
Student@15db9742
1
Why do we need this ?
Suppose we have
class Student{
int id;
Student(int id){
id = id;
Problem:
Here,
id = id;
means
Correct way
class Student{
int id;
Student(int id){
[Link] = id;
2
}
Meaning
Uses of this
The this keyword has five major uses.
class Student{
int age;
Student(int age){
[Link] = age;
void display(){
[Link](age);
3
}
Output
20
Explanation
[Link]
Instance Variable
age
Local Variable
class Demo{
void show(){
[Link]("Show");
void display(){
[Link]();
4
Output
Show
Actually,
[Link]();
is the same as
show();
this();
Example
class Student{
Student(){
this(100);
[Link]("Default");
Student(int x){
[Link](x);
5
}
Output
100
Default
Execution
Student()
this(100)
Student(int)
Back to Student()
Rules of this()
✔ Used to call another constructor.
Wrong
Student(){
[Link]("Hello");
this(10);
6
Compile-Time Error
Correct
Student(){
this(10);
[Link]("Hello");
class Demo{
void show(){
display(this);
class Demo{
Demo show(){
7
return this;
Example
class Test{
[Link](this);
Compile-Time Error
Why?
Because
Since this refers to the current object, and static methods execute without creating an object, there is
no current object.
Hence,
this
❌ Not Allowed
8
Memory Trick
Object Exists
this Exists
No Object
No this
class Student{
void display(){
[Link](age);
[Link]([Link]);
Output
30
20
Explanation
9
age
Local Variable
[Link]
Instance Variable
this vs super
this super
A) Parent Object
B) Current Object
C) JVM
D) Class
Answer: B
10
MCQ 2
Can this be used inside a static method?
A) Yes
B) No
C) Only in Java 8
D) Only in constructors
Answer: B
Explanation:
MCQ 3
Which statement is correct?
[Link] = id;
D) None
Answer: B
MCQ 4
How many major uses of this are commonly discussed?
A) 2
11
B) 3
C) 5
D) 6
Answer: C
MCQ 5
Which statement invokes another constructor?
A)
super();
B)
this();
C)
new();
D)
call();
Answer: B
MCQ 6
Where should this() be written?
A) Anywhere
B) Last statement
12
D) Inside main()
Answer: C
MCQ 7
Can this be returned from a method?
A) Yes
B) No
C) Only in constructors
Answer: A
MCQ 8
Output
class Test{
int x=10;
void show(){
[Link](this.x);
A) 0
B) 10
C) Error
D) null
13
Answer: B
MCQ 9
Output
class Test{
int x;
Test(int x){
this.x=x;
Purpose of this.x ?
A) Local Variable
B) Instance Variable
C) Static Variable
D) Method
Answer: B
MCQ 10
Which is FALSE?
14
Answer: C
MCQ 11
Which keyword passes the current object as a parameter?
A) super
B) new
C) this
D) static
Answer: C
MCQ 12
Output
class Test{
Test(){
this(5);
[Link]("A");
Test(int x){
[Link](x);
new Test();
15
}
A)
A
5
B)
5
A
C)
D) Error
Answer: B
[Link](this);
Compile-Time Error
❌ Wrong
Student(){
[Link]("Hi");
16
this(10);
❌ Wrong
id=id;
✔ Correct
[Link]=id;
Memory Tricks
Remember
this
Current Object
Remember
[Link]
Instance Variable
Remember
17
this()
Current Constructor
Remember
Static Method
No Object
No this
One-Minute Revision
✔ this refers to the current object.
✔ [Link] = id; resolves the conflict between instance and local variables.
18
TCS Tip: Questions involving [Link] = id , this() constructor calls, constructor execution order,
and why this is not allowed in static methods are asked very frequently in TCS IPA Java assessments.
19
CORE JAVA FOR TCS IPA – Chapter 10: static
Keyword (Detailed Notes + MCQs)
What is static ?
The static keyword belongs to the class, not to individual objects.
It means there is only one copy of the member, shared by all objects.
Without static :
With static :
Without static
class Student{
Memory stores
KIT
KIT
KIT
1
KIT
1000 Times
Memory is wasted.
Now
class Student{
Memory
Student Class
college = KIT
s1
s2
s3
s4
1. Static Variable
2. Static Method
2
3. Static Block
4. Static Nested Class (Advanced)
1. Static Variable
Also called a Class Variable.
Example
class Student{
Creating Objects
Memory
college
KIT
s1
s2
3
Accessing Static Variables
Preferred way
[Link]
Although
[Link]
also works,
class Student{
[Link]([Link]);
[Link]([Link]);
Output
4
KIT
KIT
2. Static Method
A static method belongs to the class.
Example
class Demo{
[Link]("Hello");
Calling
[Link]();
Output
Hello
No object required.
main()
5
Hence,
main() must be
static
class Demo{
[Link](x);
Output
6
10
class Demo{
int x = 10;
[Link](x);
Compile-Time Error
Reason
No object exists.
Correct
class Demo{
int x = 10;
[Link](d.x);
7
}
Now it works.
Memory Trick
Static Method
No Object
No Instance Variable
class Demo{
void display(){
display();
Compile-Time Error
Correct
8
Demo d = new Demo();
[Link]();
Static Block
A static block is used to initialize static data.
Syntax
class Demo{
static{
[Link]("Static Block");
class Demo{
static{
[Link]("Static Block");
[Link]("Main Method");
9
Output
Static Block
Main Method
Reason
class Demo{
static{
[Link]("First");
static{
[Link]("Second");
Output
First
Second
10
Execution Order
One of the most important TCS questions.
Suppose
class Demo{
static{
[Link]("Static");
Demo(){
[Link]("Constructor");
[Link]("Main");
new Demo();
Output
Static
Main
Constructor
Execution Order
Static Block
11
Main Method
Constructor
Memory Trick
Load Class
Static Block
main()
Object Creation
Constructor
Static vs Instance
Static Instance
12
Static vs Non-Static Method
Static Method Non-Static Method
Cannot access instance members directly Can access both static and instance members
A) Object
B) Class
C) Constructor
D) Method
Answer: B
MCQ 2
Static variables are also called
A) Instance Variables
B) Local Variables
C) Class Variables
D) Global Variables
Answer: C
13
MCQ 3
How many copies of a static variable exist?
D) Unlimited
Answer: C
MCQ 4
Can a static method access an instance variable directly?
A) Yes
B) No
D) Only in Java 8
Answer: B
MCQ 5
Why is main() declared as static?
A) To increase speed
C) To save memory
Answer: B
14
MCQ 6
Can a static method use this ?
A) Yes
B) No
C) Only in constructors
D) Only in interfaces
Answer: B
MCQ 7
When does a static block execute?
A) After constructor
Answer: C
MCQ 8
How many times does a static block execute?
C) Only once
D) Never
Answer: C
15
MCQ 9
Output
class Test{
static{
[Link]("A");
[Link]("B");
A)
B)
C) Error
D) Nothing
Answer: A
MCQ 10
Output
16
class Test{
static{
[Link]("Static");
Test(){
[Link]("Constructor");
[Link]("Main");
new Test();
A)
Main
Static
Constructor
B)
Static
Main
Constructor
C)
17
Constructor
Static
Main
D) Error
Answer: B
MCQ 11
Which statement is TRUE?
Answer: C
MCQ 12
Which member is shared among all objects?
A) Instance Variable
B) Local Variable
C) Static Variable
D) Constructor
Answer: C
18
MCQ 13
Can constructors be static?
A) Yes
B) No
C) Only private
D) Only public
Answer: B
MCQ 14
Which of the following is NOT allowed in a static method?
C) Using this
D) Creating an object
Answer: C
MCQ 15
Execution order is
A)
Constructor
Main
19
Static Block
B)
Main
Constructor
Static Block
C)
Static Block
Main
Constructor
D)
Constructor
Static Block
Main
Answer: C
20
Common Mistakes Asked in TCS
❌ Wrong
[Link](this);
Compile-Time Error
❌ Wrong
[Link](age);
Need an object.
Memory Tricks
Remember
static
Class
21
↓
One Copy
Shared
Remember
Static Method
No Object
No this
No Instance Variables
Remember
Execution Order
Static Block
Main
Constructor
22
One-Minute Revision
✔ static belongs to the class, not the object.
✔ Execution order:
TCS Tip: Focus on execution order, static block output questions, static vs instance members, and why
main() is static. These are among the most frequently asked Java MCQ topics in TCS IPA.
23
CORE JAVA FOR TCS IPA
Chapter 11 | Inheritance — Detailed Notes & MCQs
1. What is Inheritance?
Inheritance is the process by which one class acquires the properties and methods of another class.
It promotes code reusability.
class Cat{
void eat(){
[Link]("Eating");
}
}
The same code is repeated.
Using Inheritance
class Animal{
void eat(){
[Link]("Eating");
}
}
Advantages
✔ Code Reusability
✔ Less Code
✔ Easy Maintenance
4. extends Keyword
Inheritance is achieved using extends.
Syntax
class Child extends Parent{
}
Example
class Animal{
void eat(){
[Link]("Eating");
}
}
5. Types of Inheritance
Core Java for TCS IPA — Chapter 11: Inheritance Page 2
Java supports:
✔ Single Inheritance
✔ Multilevel Inheritance
✔ Hierarchical Inheritance
Java does NOT support:
6. Multiple Inheritance
Java does NOT support multiple inheritance using classes.
// Wrong
class C extends A, B{
}
■ Compile-Time Error
Suppose class A has method show(). Classes B and C both extend A and both override show(). Now
class D inherits from both B and C — which show() should D execute? This ambiguity is the Diamond
Problem.
Java avoids this problem by not allowing multiple inheritance through classes.
interface B{
void display();
}
8. IS-A Relationship
Inheritance represents IS-A.
✔ Parent members
✔ Child members
MCQ 9. Which inheritance type has one parent and one child?
A) Single
B) Multiple
C) Hierarchical
D) Hybrid
Answer: A
MCQ 10. Which inheritance type has one parent and many children?
A) Single
B) Hierarchical
C) Multilevel
D) Multiple
Answer: B
MCQ 12. Output of: class A{ A(){ [Link]("A"); } } class B extends A{ B(){
[Link]("B"); } } public class Main{ public static void main(String[] args){ new B(); }
}
A) B then A
B) A then B
C) Error
D) Nothing
Answer: B
extends Inheritance
✔ Java does not support Multiple Inheritance through classes because of the Diamond Problem.
✔ Java supports Multiple Inheritance through interfaces using the implements keyword.
■ TCS Tip
The most frequently asked questions are: types of inheritance, the extends keyword, why multiple
inheritance is not supported by classes, multiple inheritance using interfaces, constructor execution
order, and IS-A vs HAS-A relationship.
1. What is Polymorphism?
Polymorphism means “One Name, Many Forms.” In Java, polymorphism is of 2 types:
2. Method Overloading
Definition
Method Overloading means having multiple methods with the same name but different parameter
lists in the same class.
It is called Compile-Time Polymorphism because the compiler decides which method to call.
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 1
class Demo{
void show(int a){
[Link]("Integer");
}
void show(double a){
[Link]("Double");
}
}
Output: Integer, Double
Invalid Overloading
Only changing the return type is NOT overloading.
// Wrong
int add(int a,int b){
return a+b;
}
3. Method Overriding
Definition
Method Overriding means a child class provides its own implementation of a method already present
in the parent class.
It is called Run-Time Polymorphism because the JVM decides which method to execute during
runtime.
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 2
✔ Same name
✔ Same parameters
✔ Same return type (or covariant return type)
✔ Parent-Child relationship
Example
class Animal{
void sound(){
[Link]("Animal Sound");
}
}
class B extends A{
void show(){
}
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 3
}
■ Compile-Time Error
2. private Method
Private methods are not inherited. Hence, they cannot be overridden.
3. static Method
Static methods belong to the class. They are hidden, not overridden.
class A{
static void show(){
[Link]("A");
}
}
class B extends A{
static void show(){
[Link]("B");
}
}
This is called Method Hiding, not overriding.
6. Overloading vs Overriding
Method Overloading Method Overriding
7. Method Hiding
Static methods cannot be overridden. Instead, they are hidden.
class A{
static void show(){
[Link]("A");
}
}
class B extends A{
static void show(){
[Link]("B");
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 4
}
}
This is Method Hiding.
8. @Override Annotation
@Override
void show(){
}
Benefits
✔ Improves readability
✔ Compiler checks overriding errors
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 5
9. Frequently Asked TCS MCQs
MCQ 1. Method Overloading is
A) Runtime Polymorphism
B) Compile-Time Polymorphism
C) Dynamic Binding
D) None
Answer: B
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 6
MCQ 7. Can constructors be overridden?
A) Yes
B) No
C) Only default constructor
D) Only parameterized constructor
Answer: B
MCQ 9. Output of: class A{ void show(){ [Link]("A"); } } class B extends A{ void
show(){ [Link]("B"); } } public class Main{ public static void main(String[] args){ A
obj=new B(); [Link](); } }
A) A
B) B
C) Compile Error
D) Runtime Error
Answer: B
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 7
MCQ 13. Which statement is TRUE?
A) private methods can be overridden.
B) final methods can be overridden.
C) static methods participate in method hiding.
D) Constructors are overridden.
Answer: C
MCQ 15. Which of the following is NOT method overloading? int add(int a,int b) and double
add(int a,int b)
A) Valid overloading
B) Invalid because only return type changes
C) Runtime polymorphism
D) Constructor overloading
Answer: B
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 8
10. Common Mistakes Asked in TCS
■ Wrong:
int add(int a,int b){
}
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 9
Return Type Can differ (not alone) Same / Covariant
TCS Tip: This chapter is one of the top 5 most important Java topics for TCS IPA. Be especially
confident with the comparison table and output-based questions involving overridden methods.
Core Java for TCS IPA — Chapter 12: Method Overloading & Overriding Page 10
CORE JAVA FOR TCS IPA
Chapter 13 | Polymorphism — Detailed Notes & MCQs
1. What is Polymorphism?
Polymorphism means “One Name, Many Forms.”
In Java, the same method name can behave differently depending on the situation.
Example: A person can be a Student, an Employee, and a Father — the same person, different
roles. Similarly, one method can have different behaviors.
2. Types of Polymorphism
Java supports two types of polymorphism:
3. Compile-Time Polymorphism
Also called: Static Binding, Early Binding, Method Overloading
Example
class Calculator{
int add(int a,int b){
return a+b;
}
int add(int a,int b,int c){
return a+b+c;
}
public static void main(String[] args){
Calculator c=new Calculator();
[Link]([Link](10,20));
[Link]([Link](10,20,30));
}
}
Output: 30, 60
4. Run-Time Polymorphism
Also called: Dynamic Binding, Late Binding, Method Overriding
Example
class Animal{
void sound(){
[Link]("Animal Sound");
}
}
Reason: Reference type = Animal, Object type = Dog — JVM executes Dog's method.
Reference → Animal
Object → Dog
7. Advantages of Polymorphism
✔ Code Reusability
✔ Flexibility
✔ Loose Coupling
✔ Easy Maintenance
✔ Extensibility
8. Real-Life Example
Class Animal
Method sound()
Dog → Bark
Cat → Meow
Cow → Moo
Same method, different outputs — this is polymorphism.
Upcasting
Animal a = new Dog();
Called Upcasting. Automatic. Frequently asked in TCS.
Downcasting
Animal a = new Dog();
Dog d = (Dog)a;
Method Calls
Animal a = new Dog();
[Link]();
Dog's method executes.
class B extends A{
int x=20;
}
A obj=new B();
[Link](obj.x);
Output: 10
Reason: Variables use Reference Type. Methods use Object Type. Very Important TCS MCQ.
MCQ 11. Output of: class A{ int x=10; } class B extends A{ int x=20; } A obj=new B();
[Link](obj.x);
A) 20
B) 10
C) Error
D) Runtime Error
Answer: B
Explanation: Variables are resolved using the reference type, not the object type.
Reference Variable —
Object Method —
✔ Java has 2 types: Compile-Time (Method Overloading) and Run-Time (Method Overriding).
What is Encapsulation?
Encapsulation is the process of binding (wrapping) data (variables) and methods into a single unit.
In Java, a class is an example of encapsulation because it contains both variables and methods.
Definition
Encapsulation means:
Binding data and methods together into a single unit while protecting the data from
unauthorized access.
Example
int age;
1
[Link] = -50;
Here,
---
✔ **Private Variables**
Memory Trick
```text id="4c19xa"
Private Data
Getter
Read Data
Setter
Update Data
2
Private Variables
A private variable cannot be accessed directly outside the class.
Example
Wrong
```java id="e4z8v1"
Student s = new Student();
[Link] = 20;
Compile-Time Error
Reason
age is private.
Getter Method
Getter returns the value of a private variable.
Syntax
return age;
Example
3
```java id="6c7q0m"
class Student{
return age;
Output
```text id="j8l6eq" 20
---
# Setter Method
Syntax
```java id="3n8v4f"
public void setAge(int age){
[Link] = age;
Example
[Link] = age;
4
}
---
# Complete Example
```java id="5k4z1c"
class Student{
[Link] = age;
return age;
[Link](21);
[Link]([Link]());
Output
```text id="0m8v2p" 21
---
5
# Encapsulation Diagram
```text id="v3d1pt"
User
Setter
Private Variable
Getter
User
Advantages of Encapsulation
✔ Data Hiding
✔ Better Security
✔ Easy Maintenance
✔ Improves Reusability
6
Encapsulation Data Hiding
Memory Trick
Data Hiding
Private Variables
---
Example
```java id="1f8n3v"
class Student{
[Link] = age;
7
return age;
Naming Convention
Getter
Setter
```java id="7m6z1a"
setVariableName()
Example
setAge()
getName()
setName()
---
### MCQ 1
Encapsulation means
A) Hiding methods
8
C) Multiple inheritance
D) Method overloading
**Answer:** B
---
### MCQ 2
A) Public Variables
B) Private Variables
C) Static Variables
D) Final Variables
**Answer:** B
---
### MCQ 3
A) Constructor
C) Main Method
D) Static Method
**Answer:** B
---
### MCQ 4
A) public
B) protected
9
C) private
D) static
**Answer:** C
---
### MCQ 5
A) Update data
B) Read data
C) Delete data
D) Hide data
**Answer:** B
---
### MCQ 6
A) Read data
B) Update data
C) Delete data
D) Create objects
**Answer:** B
---
### MCQ 7
Output
```java id="6z2t5k"
class Test{
10
public int getX(){
return x;
[Link]([Link]());
A) 10
B) 0
C) Error
D) null
Answer: A
MCQ 8
A) Yes
B) No
C) Only in subclasses
Answer: B
11
MCQ 9
A) Public Variables
B) Encapsulation
C) Static Methods
D) Constructors
Answer: B
MCQ 10
Which is TRUE?
Answer: B
MCQ 11
A) age()
B) getAge()
C) setAge()
D) fetch()
Answer: B
12
MCQ 12
A) age()
B) getAge()
C) setAge()
D) update()
Answer: C
MCQ 13
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Overloading
Answer: B
MCQ 14
A) Yes
B) No
C) Only in Java 8
D) Only in interfaces
Answer: A
13
MCQ 15
Answer: C
✔ Correct
```java id="3v8m1q"
[Link](25);
Memory Tricks
Remember
14
```text id="8c5j1r" Encapsulation
Binding
Data + Methods
---
Remember
```text id="4n7w2k"
private
Data Hiding
Remember
Get
Read
---
Remember
```text id="2d9x6f"
Setter
Set
15
↓
Update
One-Minute Revision
✔ Encapsulation means binding data and methods together into one unit.
✔ Encapsulation provides data hiding, security, maintainability, and better control over data.
Concept Remember
• What is Encapsulation?
• How is Encapsulation achieved?
• Difference between Getter and Setter.
• Can private variables be accessed directly?
• Which access modifier provides data hiding?
• Advantages of Encapsulation.
16
CORE JAVA FOR TCS IPA – Chapter 15: Abstraction
(Detailed Notes + MCQs)
What is Abstraction?
Abstraction means hiding implementation details and showing only essential functionality to the user.
Example:
This is Abstraction.
Definition
Abstraction is the process of hiding implementation details and exposing only the necessary features
to the user.
With abstraction,
Advantages:
✔ Security
✔ Simplicity
1
✔ Code Reusability
✔ Easy Maintenance
✔ Loose Coupling
✔ Abstract Class
✔ Interface
Memory Trick
Abstract Class
Interface
---
# Abstract Class
Syntax
```java id="7r2g6m"
abstract class Animal{
Wrong
2
```java id="2p4v9d" Animal a = new Animal();
Compile-Time Error
---
# Abstract Method
Syntax
```java id="9n8j2f"
abstract void sound();
Example
---
```java id="8d3h7m"
abstract class Animal{
void sound(){
[Link]("Dog Barks");
3
}
[Link]();
Output
---
Yes.
Example
```java id="6j2y5t"
abstract class Animal{
void eat(){
[Link]("Eating");
✔ Abstract Methods
✔ Normal Methods
4
Can an Abstract Class Have a Constructor?
Yes.
Example
Animal(){
[Link]("Constructor Called");
---
Yes.
Example
```java id="2q6k8n"
abstract class Animal{
5
✔ Can have variables
Interface
An Interface is a blueprint that specifies what a class must do.
Syntax
void sound();
---
# Implementing an Interface
Example
```java id="7h3n1y"
interface Animal{
void sound();
[Link]("Dog Barks");
6
}
Output
---
# Interface Features
✔ No Constructors
✔ No Objects
---
From Java 8,
✔ Default Methods
✔ Static Methods
Example
```java id="3w8f6r"
interface Test{
[Link]("Default");
7
[Link]("Static");
✔ Private Methods
Example
---
No.
Example
```java id="6x1p8b"
interface Demo{
Demo(){
8
Compile-Time Error
Reason
Can have normal methods Can have default & static methods (Java 8+)
extends vs implements
```java id="5p7c2d" class Dog extends Animal
Used for
---
```java id="4y1n8s"
class Dog implements Animal
Used for
9
Interface
interface B{
Valid
---
### MCQ 1
Abstraction means
A) Data Hiding
C) Method Overloading
D) Inheritance
**Answer:** B
---
### MCQ 2
10
A) Abstract Class
B) Interface
C) Both A and B
D) Constructor
**Answer:** C
---
### MCQ 3
A) Yes
B) No
C) Only private
D) Only protected
**Answer:** A
---
### MCQ 4
A) Yes
B) No
**Answer:** B
---
### MCQ 5
11
A) Yes
B) No
**Answer:** A
---
### MCQ 6
A) Yes
B) No
C) Only one
D) Only protected
**Answer:** A
---
### MCQ 7
A) Yes
B) No
C) Only in Java 8
**Answer:** B
---
### MCQ 8
12
A) extends
B) inherits
C) implements
D) super
**Answer:** C
---
### MCQ 9
A) Abstract Methods
C) Constructors
D) Private Variables
**Answer:** B
---
### MCQ 10
A) Constructors
B) Private Methods
C) Final Methods
D) Instance Variables
**Answer:** B
---
### MCQ 11
13
A) Interfaces can have constructors.
**Answer:** B
---
### MCQ 12
A) interface
B) class
C) abstract
D) final
**Answer:** C
---
### MCQ 13
Output
```java id="7z5r3n"
abstract class A{
class B extends A{
void show(){
[Link]("Hello");
14
public class Main{
[Link]();
A) Hello
B) Error
C) Nothing
D) null
Answer: A
MCQ 14
B) Constructor in Interface
Answer: B
MCQ 15
A) private
15
B) public static final
C) protected
D) instance variables
Answer: B
---
---
✔ Use
```java id="6k4x1p"
implements
Memory Tricks
Remember
16
Hide Implementation
Show Functionality
---
Remember
```text id="3p6n8t"
Abstract Class
Constructor ✔
Normal Methods ✔
Abstract Methods ✔
Remember
Constructor ❌
---
Remember
```text id="1y7w5c"
Abstract Class
17
extends
Interface
implements
One-Minute Revision
✔ Abstraction hides implementation details and exposes only essential functionality.
• Constructors
• Normal methods
• Abstract methods
• Variables
Constructor ✅ Yes ❌ No
18
Topic Abstract Class Interface
• What is Abstraction?
• How is Abstraction achieved?
• Can an abstract class have a constructor? → ✅ Yes
• Can an interface have a constructor? → ❌ No
• Java 8 interface features → default & static methods
• Java 9 interface feature → private methods
• Difference between Abstract Class and Interface
• Which keyword is used with interfaces? → implements
19
CORE JAVA FOR TCS IPA – Chapter 16: Access
Modifiers (Detailed Notes + MCQs)
1. private
2. default (Package-Private)
3. protected
4. public
This is one of the most frequently asked TCS IPA Java MCQ topics.
✔ Data Security
✔ Encapsulation
✔ Controlled Access
✔ Information Hiding
1
private
default
protected
public
---
# 1. private
Example
```java id="5f2w8c"
class Student{
void show(){
[Link](age);
Wrong
[Link]([Link]);
Compile-Time Error
Reason
`age` is private.
---
2
# private Access Table
Memory Trick
```text id="9m3d6r"
private
Only My Class
2. Default (Package-Private)
If no access modifier is specified,
Example
int age;
---
Memory Trick
3
```text id="4r8y2n"
default
3. protected
Protected members are accessible
✔ In subclasses
Example
[Link]("Sound");
Subclass
```java id="3q5m7v"
class Dog extends Animal{
void display(){
sound();
4
}
Works correctly.
Memory Trick
---
# 4. public
Example
```java id="6v7h3k"
public class Student{
[Link]("Hello");
5
public Access Table
Same Class Same Package Subclass Other Package
Memory Trick
Accessible Everywhere
---
---
# Accessibility Order
```text id="7n8w2j"
private
default
protected
6
public
As we move downward,
Accessibility increases.
---
Answer
```text id="8q9y5c"
public
✔ public
✔ default
Not Allowed
❌ private
❌ protected
Example
Correct
7
```java id="9p6d4t" public class Demo{
Correct
```java id="4c2n8f"
class Demo{
Wrong
Compile-Time Error
---
```text id="6b1x4n"
private
default
protected
public
A) public
8
B) protected
C) private
D) default
Answer: C
MCQ 2
A) protected
B) default
C) public
D) private
Answer: C
MCQ 3
A) protected
B) public
C) private
D) default
Answer: C
MCQ 4
9
B) Same Package
D) Everywhere
Answer: B
MCQ 5
Answer: C
MCQ 6
A) protected
B) default
C) public
D) private
Answer: C
MCQ 7
A) public
10
B) private
C) protected
D) default
Answer: B
MCQ 8
D) all four
Answer: B
MCQ 9
A) Yes
B) No
C) Only in Java 8
Answer: B
MCQ 10
A) Yes
11
B) No
D) Only interfaces
Answer: B
MCQ 11
Answer: C
MCQ 12
A) Directly
C) Never
Answer: B
MCQ 13
A)
12
```text id="8f5z2j" public
protected
default
private
B)
```text id="3r7v1n"
private
default
protected
public
C)
private
protected
public
D)
```text id="9x2c6w"
protected
private
public
default
Answer: B
13
MCQ 14
A) private
B) default
C) protected
D) public
Answer: B
MCQ 15
Answer: B
14
Compile-Time Error.
Memory Tricks
Remember
Only My Class
---
Remember
```text id="4k1v9m"
default
Same Package
Remember
---
Remember
```text id="9d6w2t"
public
15
Everywhere
Remember
private
default
protected
public ```
One-Minute Revision
✔ Java has 4 Access Modifiers:
• private
• default
• protected
• public
✔ protected → Accessible in the same package and in subclasses (including other packages through
inheritance).
16
🎯 TCS IPA Memory Shortcut
Modifier Access
public Everywhere
private ✅ ❌ ❌ ❌
default ✅ ✅ ❌ ❌
public ✅ ✅ ✅ ✅
17
CORE JAVA FOR TCS IPA – Chapter 17: final
Keyword (Detailed Notes + MCQs)
✔ Variable
✔ Method
✔ Class
Memory Trick
Cannot Change
---
# Uses of `final`
---
# 1. final Variable
1
After initialization, its value **cannot be changed**.
Example
```java id="n3x8v2"
class Demo{
void display(){
[Link](x);
Output
```text id="r2m6q8" 10
---
```java id="h5w9c4"
class Demo{
void show(){
x = 20;
Output
2
Reason
---
Example
```java id="k4f7d9"
class Demo{
final int x;
Demo(){
x = 100;
Valid
Wrong
```java id="p8z3w6"
s = new Student();
3
Compile-Time Error
Valid
Memory Trick
```text id="d9q7b3"
final Reference
Reference Fixed
2. final Method
A final method cannot be overridden by a child class.
Example
[Link]("Animal Sound");
Wrong
```java id="g7m9c5"
class Dog extends Animal{
4
void sound(){
[Link]("Dog Bark");
Output
Reason
---
Yes.
Example
```java id="e2w7h4"
class Demo{
Valid
Reason
5
3. final Class
A final class cannot be inherited.
Example
Wrong
```java id="q3m6v9"
class Dog extends Animal{
Output
Reason
---
```java id="j5c2x8"
public final class String{
Therefore,
6
}
Compile-Time Error
Reason
String is final.
---
Reasons
✔ Security
✔ Immutability
✔ Better Performance
✔ Reliable Hashing
---
Memory Trick
```text id="z7m5d2"
final
Keyword
finally
7
↓
Exception Handling
finalize()
Garbage Collection
[Link](MAX);
Valid
---
No.
Wrong
```java id="w6n8p4"
final Demo(){
Compile-Time Error
Reason
8
Constructors are not inherited,
Wrong
Compile-Time Error
Reason
---
### MCQ 1
A) Increase speed
B) Restrict modification
C) Hide data
D) Create objects
**Answer:** B
---
### MCQ 2
9
A final variable
A) Can be changed
D) Is always private
**Answer:** B
---
### MCQ 3
A final method
A) Can be overridden
B) Cannot be overridden
C) Cannot be overloaded
D) Must be abstract
**Answer:** B
---
### MCQ 4
A final class
A) Can be inherited
B) Cannot be inherited
C) Must be abstract
**Answer:** B
---
### MCQ 5
10
Which class is final in Java?
A) Object
B) System
C) String
D) Scanner
**Answer:** C
---
### MCQ 6
A) Yes
B) No
C) Only in interfaces
**Answer:** A
---
### MCQ 7
A) Yes
B) No
**Answer:** B
---
### MCQ 8
11
Can abstract methods be final?
A) Yes
B) No
**Answer:** B
---
### MCQ 9
**Answer:** C
---
### MCQ 10
Output
```java id="f8k3v7"
final int x = 10;
x = 20;
A) 20
B) 10
C) Compile-Time Error
D) Runtime Error
12
Answer: C
MCQ 11
A) static
B) final
C) abstract
D) private
Answer: B
MCQ 12
A) Inheritance
B) Exception Handling
C) Interfaces
D) Threads
Answer: B
MCQ 13
finalize() is called by
A) Compiler
C) Constructor
D) Main Method
13
Answer: B
MCQ 14
A) Variable
B) Method
C) Class
D) Package
Answer: D
MCQ 15
Answer: C
x = 30;
Compile-Time Error.
---
14
❌ Trying to extend String.
```java id="c4v2m8"
class MyString extends String{
Compile-Time Error.
❌ Writing
Compile-Time Error.
---
# Memory Tricks
Remember
```text id="x5m8r1"
final Variable
Remember
Cannot Override
---
Remember
15
```text id="d3w7k4"
final Class
Cannot Inherit
Remember
final Class
---
Remember
```text id="m8t5c2"
final
Keyword
finally
Exception Block
finalize()
Garbage Collection
One-Minute Revision
✔ final is used to restrict modification.
16
✔ final Variable → Value cannot be changed after initialization.
✔ Difference:
• final → Keyword
• finally → Exception Handling Block
• finalize() → Garbage Collection Method
17
CORE JAVA FOR TCS IPA – Chapter 18: Exception
Handling (Detailed Notes + MCQs)
Exception Handling is the mechanism used to handle such errors so that the program can continue
executing normally.
Example:
int a = 10;
int b = 0;
[Link](a / b);
Output
ArithmeticException
Advantages
1
• Improves readability.
• Helps in debugging.
• Maintains normal program flow.
Exception Hierarchy
One of the most important TCS IPA MCQs.
Object
│
Throwable
│
┌───────────────┐
│ │
Error Exception
│
RuntimeException
Memory Trick
Throwable
Exception
RuntimeException
Types of Exceptions
Java exceptions are mainly divided into:
1. Checked Exceptions
2. Unchecked Exceptions
2
1. Checked Exceptions
Checked exceptions are checked by the compiler during compilation.
Also called
✔ Compile-Time Exceptions
Examples
• IOException
• SQLException
• FileNotFoundException
• ClassNotFoundException
Example
2. Unchecked Exceptions
Unchecked exceptions occur during runtime.
Also called
✔ Runtime Exceptions
Examples
• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException
3
• NumberFormatException
Example
int a = 10;
[Link](a / 0);
Output
ArithmeticException
NullPointerException
Occurs when a null reference accesses an object.
Example
String s = null;
[Link]([Link]());
Output
NullPointerException
ArrayIndexOutOfBoundsException
Occurs when an invalid array index is accessed.
4
Example
[Link](arr[5]);
Output
ArrayIndexOutOfBoundsException
NumberFormatException
Occurs when converting an invalid string into a number.
Example
[Link]("ABC");
Output
NumberFormatException
try
catch
finally
throw
throws
5
try Block
The code that may generate an exception is placed inside the try block.
Example
try{
int a = 10/0;
catch Block
The catch block handles the exception.
Example
try{
int a = 10/0;
catch(ArithmeticException e){
Output
6
finally Block
The finally block executes whether an exception occurs or not.
Example
try{
[Link]("Try");
finally{
[Link]("Finally");
Output
Try
Finally
Exception
[Link](0);
Example
try{
[Link](0);
7
finally{
[Link]("Finally");
Output
(No Output)
Reason
Answer: Almost Yes, except when the JVM exits (e.g., [Link]() ).
throw Keyword
Used to explicitly throw an exception.
Example
throws Keyword
Used in the method declaration to indicate that the method may throw an exception.
Example
8
void display() throws IOException{
Memory Trick
throw
throws
Declares Exceptions
throw vs throws
throw throws
try{
catch(ArithmeticException e){
9
catch(NullPointerException e){
catch(Exception e){
Always place
Specific Exception
General Exception
Otherwise,
Compile-Time Error.
Exception Propagation
If an exception is not handled,
Example
method3()
method2()
method1()
main()
10
Checked vs Unchecked Exceptions
Checked Exception Unchecked Exception
Compile-Time Runtime
IOException ArithmeticException
SQLException NullPointerException
Error vs Exception
Error Exception
StackOverflowError IOException
A) Speed up execution
C) Increase memory
D) Create objects
Answer: B
11
MCQ 2
A) Object
B) Throwable
C) Exception
D) Error
Answer: B
MCQ 3
A) ArithmeticException
B) NullPointerException
C) IOException
D) NumberFormatException
Answer: C
MCQ 4
A) IOException
B) SQLException
C) ArithmeticException
D) FileNotFoundException
Answer: C
12
MCQ 5
B) Divide by zero
Answer: B
MCQ 6
C) File is missing
Answer: B
MCQ 7
A) try
B) catch
C) Both A and B
D) throw
Answer: C
13
MCQ 8
A) try
B) catch
C) finally
D) throw
Answer: C
MCQ 9
A) throws
B) throw
C) finally
D) catch
Answer: B
MCQ 10
A) throw
B) throws
C) catch
D) try
Answer: B
14
MCQ 11
[Link]("ABC");
A) IOException
B) NumberFormatException
C) ArithmeticException
D) SQLException
Answer: B
MCQ 12
Answer: B
MCQ 13
A) Divide by zero
B) NullPointerException
C) [Link]()
D) IOException
15
Answer: C
MCQ 14
String s = null;
[Link]([Link]());
A) IOException
B) NullPointerException
C) ArithmeticException
D) NumberFormatException
Answer: B
MCQ 15
A)
Exception
Throwable
Object
B)
Object
16
Throwable
Exception
RuntimeException
C)
RuntimeException
Throwable
Object
D)
Object
Exception
Throwable
Answer: B
17
❌ Confusing throw and throws .
Memory Tricks
Remember
Throwable
Exception
RuntimeException
Remember
Checked
Compile-Time
IOException
SQLException
18
Remember
Unchecked
Runtime
ArithmeticException
NullPointerException
ArrayIndexOutOfBoundsException
NumberFormatException
Remember
throw
Throw One
throws
Declare Many
Remember
finally
19
Except [Link]()
One-Minute Revision
✔ Exception = Unexpected event during program execution.
• try
• catch
• finally
• throw
• throws
✔ finally executes almost always, except when the JVM terminates using [Link]() .
Keyword Purpose
20
• Checked vs Unchecked Exceptions
• Difference between throw and throws
• Common Runtime Exceptions
• Does finally always execute? → Almost Yes, except [Link]()
• Exception hierarchy
• Which exceptions are checked and unchecked?
21
CORE JAVA FOR TCS IPA – Chapter 19: Java
Collections Framework (Detailed Notes + MCQs)
Collections provide:
• Dynamic Size
• Easy Searching
• Sorting
• Insertion
• Deletion
• Better Performance
❌ Fixed Size
❌ Difficult insertion/deletion
Advantages
✔ Dynamic Size
✔ Built-in Algorithms
✔ Better Performance
1
Collection Framework Hierarchy
One of the most important TCS IPA MCQs.
```text id="7v2kp9" Iterable │ Collection │ ├── List ├── Set └── Queue
Memory Trick
```text id="2m8cr4"
Collection
List
Set
Queue
Map (Separate)
List Interface
A List stores elements in insertion order.
Features
✔ Duplicates Allowed
✔ Order Maintained
✔ Index-Based Access
Example
```text id="8d4nh6" 10
2
20
20
30
---
# ArrayList
Features
✔ Dynamic Array
✔ Allows Duplicates
Example
```java id="4k5bz2"
ArrayList<String> list = new ArrayList<>();
[Link]("A");
[Link]("B");
[Link]("A");
[Link](list);
Output
---
# LinkedList
3
Implemented using a doubly linked list.
Features
✔ Fast Insertion
✔ Fast Deletion
✔ Maintains Order
✔ Allows Duplicates
---
# ArrayList vs LinkedList
| ArrayList | LinkedList |
|------------|------------|
| Dynamic Array | Doubly Linked List |
| Fast Random Access | Slow Random Access |
| Slow Insertion | Fast Insertion |
| Less Memory | More Memory |
---
# Vector
Features
✔ Thread Safe
✔ Synchronized
---
# ArrayList vs Vector
| ArrayList | Vector |
|------------|--------|
| Not Thread Safe | Thread Safe |
4
| Faster | Slower |
| Not Synchronized | Synchronized |
---
# Stack
Stack follows
```text id="9t6fp3"
LIFO
Last In
First Out
Example
10
20
30
Pop
30
Applications
✔ Undo Operation
✔ Browser History
✔ Function Calls
---
# Queue
Queue follows
5
```text id="1z8vr5"
FIFO
First In
First Out
Example
```text id="5j3cw9" 10
20
30
Remove
10
Applications
✔ Printer Queue
✔ Ticket Booking
✔ Scheduling
---
# Set Interface
Features
✔ No Duplicates
✔ No Index
---
# HashSet
6
Features
✔ No Duplicates
✔ Unordered
✔ Fast
Example
```java id="6r1tm8"
HashSet<Integer> set = new HashSet<>();
[Link](10);
[Link](20);
[Link](10);
[Link](set);
Output
Duplicate removed.
---
# LinkedHashSet
Features
✔ No Duplicates
---
# TreeSet
Features
✔ No Duplicates
7
✔ Sorted Order
✔ Implements NavigableSet
Example
```text id="7k5wp2"
10
20
Output
10
20
Automatically sorted.
Set Comparison
HashSet LinkedHashSet TreeSet
Map Interface
A Map stores data in Key-Value Pairs.
Example
8
102 → Rahul
103 → Amit
---
# HashMap
Features
✔ Key-Value Pair
✔ Unordered
✔ Fast
Example
```java id="4d7kn1"
HashMap<Integer,String> map = new HashMap<>();
[Link](1,"A");
[Link](2,"B");
[Link](null,"C");
[Link](3,null);
Valid.
9
Hashtable
Features
✔ Thread Safe
✔ No Null Key
✔ No Null Value
✔ Synchronized
HashMap vs Hashtable
HashMap Hashtable
Faster Slower
TreeMap
Features
✔ Sorted Keys
✔ No Null Key
Example
```text id="5m7dz9" 5
10
8
Output
---
# LinkedHashMap
Features
---
---
11
# Frequently Asked TCS MCQs
### MCQ 1
A) Set
B) List
C) Map
D) Queue
**Answer:** B
---
### MCQ 2
A) ArrayList
B) LinkedList
C) HashSet
D) Vector
**Answer:** C
---
### MCQ 3
A) LinkedList
B) ArrayList
C) Stack
D) Vector
**Answer:** B
12
---
### MCQ 4
A) ArrayList
B) LinkedList
C) Vector
D) Stack
**Answer:** B
---
### MCQ 5
A) Queue
B) Stack
C) LinkedList
D) TreeSet
**Answer:** B
---
### MCQ 6
A) Queue
B) Stack
C) Vector
D) TreeMap
**Answer:** A
13
---
### MCQ 7
A) HashSet
B) LinkedHashSet
C) TreeSet
D) Vector
**Answer:** B
---
### MCQ 8
A) HashSet
B) LinkedHashSet
C) TreeSet
D) Stack
**Answer:** C
---
### MCQ 9
A) TreeMap
B) LinkedList
C) HashMap
D) Hashtable
**Answer:** C
14
---
### MCQ 10
HashMap allows
A) No Null Key
**Answer:** B
---
### MCQ 11
HashMap allows
A) No Null Values
**Answer:** C
---
### MCQ 12
Hashtable allows
**Answer:** C
15
---
### MCQ 13
A) HashMap
B) Hashtable
C) LinkedHashMap
D) TreeMap
**Answer:** D
---
### MCQ 14
A) HashMap
B) Hashtable
C) LinkedHashMap
D) TreeMap
**Answer:** C
---
### MCQ 15
**Answer:** C
16
---
---
---
---
---
# Memory Tricks
Remember
```text id="6r4kb2"
List
Duplicates
Order
Remember
17
```text id="9v1tm7" Set
No Duplicates
---
Remember
```text id="4c8wp5"
Stack
LIFO
Remember
FIFO
---
Remember
```text id="8m7qz1"
HashMap
1 Null Key
Remember
18
```text id="5x3kl6" Hashtable
No Null Key
No Null Value
---
Remember
```text id="7j2pv9"
TreeMap
Sorted Keys
Remember
One-Minute Revision
✔ Collection Hierarchy → List, Set, Queue (Map is separate).
19
✔ Stack → LIFO.
✔ Queue → FIFO.
✔ Set → No Duplicates.
Stack LIFO
Queue FIFO
HashSet No Duplicates
LinkedHashSet Ordered
TreeSet Sorted
Hashtable No Nulls
20
⭐ Most Expected TCS MCQs
21
CORE JAVA FOR TCS IPA – Chapter 20: Wrapper
Classes (Detailed Notes + MCQs)
Java provides wrapper classes because Collections Framework and many Java APIs work only with objects,
not primitive data types.
Example
Object
Wrapper Class
Example
```java id="6m8k1p"
int x = 10;
Here,
1
Wrong
Compile-Time Error
Correct
```java id="2w7p4d"
ArrayList<Integer> list = new ArrayList<>();
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Autoboxing
Autoboxing is the automatic conversion of a primitive data type into its corresponding wrapper
object.
Example
Integer obj = x;
2
Java automatically converts
```text id="1k9n5v"
int
Integer
Output
---
# Manual Boxing
Before Java 5,
Example
```java id="4d6y2n"
int x = 100;
Unboxing
Unboxing is the automatic conversion of a wrapper object into a primitive data type.
Example
int x = obj;
```text id="6j1w4r"
3
Integer
int
Output
---
# Manual Unboxing
Example
```java id="2m9c5x"
Integer obj = [Link](200);
int x = [Link]();
Autoboxing vs Unboxing
Autoboxing Unboxing
Automatic Automatic
Memory Trick
Primitive
Object
4
```text id="8y4m1q"
Unboxing
Object
Primitive
Example
int x = [Link](s);
[Link](x);
Output
```text id="9m1d7w"
123
[Link]()
Converts a primitive or String into an Integer object.
Example
5
---
## intValue()
Example
```java id="1n4k9m"
Integer obj = 50;
int x = [Link]();
[Link]()
Checks whether a character is a digit.
Example
Output
```text id="7x9m4p"
true
[Link]()
Checks whether a character is a letter.
Example
Output
```text id="2k7n5r"
true
6
[Link]()
Checks whether a character is uppercase.
Example
Output
```text id="6r3v9q"
true
[Link]()
Checks whether a character is lowercase.
Example
Output
```text id="9t6w1m"
true
Example
x = 20;
Actually,
7
---
```text id="4x9k1p"
Object
Number
Byte
Short
Integer
Long
Float
Double
A) Int
B) Integer
C) Number
D) Long
Answer: B
8
MCQ 2
A) Char
B) Character
C) String
D) CharacterWrapper
Answer: B
MCQ 3
Autoboxing converts
A) Object → Primitive
B) Primitive → Object
C) String → Integer
D) Object → String
Answer: B
MCQ 4
Unboxing converts
A) Primitive → Object
B) Object → Primitive
C) String → Integer
D) Object → Object
Answer: B
9
MCQ 5
A) valueOf()
B) parseInt()
C) intValue()
D) toInt()
Answer: B
MCQ 6
A) parseInt()
B) valueOf()
C) intValue()
D) parse()
Answer: B
MCQ 7
A) Bool
B) Boolean
C) Logical
D) Binary
Answer: B
10
MCQ 8
A) Decimal
B) Double
C) Float
D) Number
Answer: B
MCQ 9
A) Long
B) Integer
C) Double
D) Number
Answer: A
MCQ 10
A) Float
B) Double
C) Decimal
D) Number
Answer: A
11
MCQ 11
A) Byte
B) Integer
C) Number
D) Character
Answer: A
MCQ 12
Answer: B
MCQ 13
A) valueOf()
B) parseInt()
C) intValue()
D) toInteger()
Answer: C
12
MCQ 14
A) Integer
B) Double
C) Float
Answer: D
MCQ 15
Answer: D
✔ Correct answer:
---
13
---
---
# Memory Tricks
Remember
```text id="7n2x5r"
Primitive
Wrapper
Integer
---
```text id="5w9v2n"
char
Character
Boolean
14
---
Remember
```text id="9q5t1v"
Autoboxing
Primitive
Object
Remember
Object
Primitive ```
One-Minute Revision
✔ Wrapper classes convert primitive data types into objects.
15
✔ Wrapper classes are immutable.
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
16
CORE JAVA FOR TCS IPA – Chapter 21: Java
Packages (Detailed Notes + MCQs)
What is a Package?
A Package is a collection of related classes, interfaces, enums, and sub-packages.
Example
Java Package
Classes
---
Without packages,
Packages provide:
✔ Better Organization
✔ Security
✔ Access Protection
1
---
# Types of Packages
## 1. Built-in Packages
Provided by Java.
Examples
- `[Link]`
- `[Link]`
- `[Link]`
- `[Link]`
- `[Link]`
- `[Link]`
---
## 2. User-Defined Packages
Created by programmers.
Example
```java id="2m4v7q"
package [Link];
1. [Link]
The most important package.
2
Examples
• String
• Object
• System
• Math
• Integer
• Character
• Thread
• Exception
Example
[Link](name);
---
```java id="9r3p8w"
[Link].*
Therefore,
• String
• System
• Math
• Object
without writing
3
Memory Trick
```text id="6v9q2k"
Automatic Import
[Link]
2. [Link]
Contains utility classes.
Common Classes
• ArrayList
• LinkedList
• HashMap
• HashSet
• Scanner
• Collections
• Arrays
• Date
• Random
Example
---
# 3. [Link]
Common Classes
- File
- FileReader
- FileWriter
- BufferedReader
4
- BufferedWriter
- PrintWriter
Example
```java id="5p8r2x"
import [Link];
Applications
✔ Reading Files
✔ Writing Files
4. [Link]
Used for database programming.
Common Classes
• Connection
• Statement
• PreparedStatement
• ResultSet
• DriverManager
Example
Applications
✔ JDBC
✔ MySQL
✔ Oracle Database
---
## [Link]
5
Used for networking.
Examples
- Socket
- URL
- ServerSocket
---
## [Link]
Introduced in Java 8.
Examples
- LocalDate
- LocalTime
- LocalDateTime
---
# Import Statement
Syntax
```java id="6m1q9v"
import [Link];
Example
---
Syntax
```java id="1t8r5d"
import [Link].*;
6
Imports all classes from the package.
Example
---
# package Keyword
Example
```java id="2v5m8r"
package student;
---
7
Use lowercase letters.
Examples
```text id="3m8v2p"
[Link]
student
[Link]
[Link]
A package is
A) A method
C) A constructor
D) A variable
8
Answer: B
MCQ 2
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: D
MCQ 3
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: C
MCQ 4
A) [Link]
B) [Link]
C) [Link]
D) [Link]
9
Answer: B
MCQ 5
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: C
MCQ 6
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: B
MCQ 7
A) import
B) package
C) class
D) new
10
Answer: B
MCQ 8
A) package
B) import
C) extends
D) implements
Answer: B
MCQ 9
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: C
MCQ 10
A) [Link]
B) [Link]
C) [Link]
D) [Link]
11
Answer: A
MCQ 11
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: C
MCQ 12
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: A
MCQ 13
A) [Link]
B) [Link]
C) [Link]
D) [Link]
12
Answer: B
MCQ 14
Answer: B
MCQ 15
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: B
✔ Not required.
---
13
❌ Thinking `Scanner` belongs to `[Link]`.
✔ Correct package
```text id="8r5m1v"
[Link]
✔ Correct package
---
# Memory Tricks
Remember
```text id="2m9q6r"
[Link]
Automatic Import
Remember
Collections
Scanner
---
Remember
14
```text id="9p1r7m"
[Link]
File Handling
Remember
Database
---
Remember
```text id="7n2v4k"
package
Create Package
import
Use Package
One-Minute Revision
✔ A Package is a collection of related classes and interfaces.
15
✔ [Link] is used for file handling.
Package Purpose
[Link] Networking
16
CORE JAVA FOR TCS IPA – Chapter 22:
Multithreading (Detailed Notes + MCQs)
What is Multithreading?
Multithreading is the process of executing multiple threads simultaneously within a single program.
Example
This is Multithreading.
With multithreading,
Advantages
✔ Better Performance
✔ Faster Execution
1
✔ Background Processing
Process vs Thread
Process Thread
Heavyweight Lightweight
Slower Faster
Runnable
Running
Waiting / Blocked
Terminated
---
2
## 1. Extending the Thread Class
Example
```java id="5k8p2m"
class MyThread extends Thread{
[Link]("Thread Running");
[Link]();
Output
---
Example
```java id="8m4q7v"
class MyThread implements Runnable{
[Link]("Thread Running");
3
}
[Link]();
Output
---
# Thread vs Runnable
---
```text id="4p8m2r"
start()
run()
sleep()
4
join()
yield()
interrupt()
start()
Starts a new thread.
Example
[Link]();
Output
---
# run()
Example
```java id="2m6k9w"
public void run(){
[Link]("Running");
5
Difference Between start() and run()
This is one of the highest-weightage TCS MCQs.
start() run()
Example
[Link]();
---
```java id="8w2m6q"
MyThread t = new MyThread();
[Link]();
No new thread.
sleep()
Pauses the current thread for a specified time.
Syntax
Meaning
6
---
# join()
Example
```java id="3v8k5m"
[Link]();
[Link]();
[Link]();
yield()
Temporarily pauses the current thread and gives other threads a chance to execute.
Example
---
# interrupt()
Example
```java id="7m4v1k"
[Link]();
Usually results in
7
---
# Thread Scheduler
---
# Thread Priority
Priority Range
```text id="5x8n3v"
1
10
Constants
Thread.NORM_PRIORITY
Thread.MAX_PRIORITY
10
8
Higher priority means a thread has a better chance of execution.
---
# Thread States
```text id="1k5v8r"
NEW
RUNNABLE
RUNNING
WAITING
TIMED_WAITING
BLOCKED
TERMINATED
Synchronization
Synchronization ensures
Purpose
Daemon Thread
A daemon thread runs in the background.
Examples
✔ Garbage Collector
✔ Background Services
9
Main Thread
Every Java program starts with
---
### MCQ 1
A thread is
A) A process
C) A package
D) An object
**Answer:** B
---
### MCQ 2
A) One
B) Two
C) Three
D) Four
**Answer:** B
---
10
### MCQ 3
A) Object
B) Thread
C) Runnable
D) Process
**Answer:** B
---
### MCQ 4
A) Serializable
B) Runnable
C) Comparable
D) Cloneable
**Answer:** B
---
### MCQ 5
A) run()
B) execute()
C) start()
D) begin()
**Answer:** C
---
11
### MCQ 6
A) main()
B) start()
C) run()
D) sleep()
**Answer:** C
---
### MCQ 7
A) join()
B) sleep()
C) yield()
D) stop()
**Answer:** B
---
### MCQ 8
A) sleep()
B) yield()
C) join()
D) interrupt()
**Answer:** C
---
12
### MCQ 9
A) stop()
B) destroy()
C) interrupt()
D) notify()
**Answer:** C
---
### MCQ 10
A) join()
B) sleep()
C) yield()
D) start()
**Answer:** C
---
### MCQ 11
**Answer:** B
---
13
### MCQ 12
A) Worker Thread
B) Daemon Thread
C) Main Thread
D) Child Thread
**Answer:** C
---
### MCQ 13
A) Main Thread
B) Daemon Thread
C) Runnable Thread
D) Worker Thread
**Answer:** B
---
### MCQ 14
A) 1
B) 5
C) 10
D) 0
**Answer:** B
---
14
### MCQ 15
**Answer:** C
---
❌ Calling
```java id="7p3k8m"
run();
✔ Only
---
---
---
15
# Memory Tricks
Remember
```text id="4x8n2p"
start()
New Thread
Remember
Normal Method
---
Remember
```text id="2k9q4w"
sleep()
Pause
Remember
Wait
---
16
Remember
```text id="1n5r2q"
yield()
Give Chance
Remember
One-Minute Revision
✔ Multithreading means executing multiple threads simultaneously.
17
✔ Thread priority:
• MIN_PRIORITY = 1
• NORM_PRIORITY = 5
• MAX_PRIORITY = 10
Method Purpose
18
CORE JAVA FOR TCS IPA – Chapter 23: File Handling
(Detailed Notes + MCQs)
Normally, data stored in variables is temporary (lost when the program ends).
Example
File
Permanent Storage
---
Applications
- Student Records
- Banking Systems
- Employee Data
- Configuration Files
- Log Files
1
---
```java id="8p5m2q"
[Link]
Example
---
```text id="2v6r1m"
File
FileReader
BufferedReader
FileWriter
BufferedWriter
1. File Class
The File class is used to represent files and directories.
It can:
✔ Create a file
✔ Delete a file
2
✔ Get file name
Example
---
| Method | Purpose |
|---------|---------|
| createNewFile() | Creates a new file |
| exists() | Checks whether file exists |
| delete() | Deletes the file |
| getName() | Returns file name |
| getPath() | Returns file path |
| length() | Returns file size |
| mkdir() | Creates a directory |
Example
```java id="7x4m1q"
File file = new File("[Link]");
[Link]([Link]());
2. FileReader
Used to read character data from a file.
3
Example
---
# 3. BufferedReader
Advantages
Example
```java id="6r2k9m"
BufferedReader br =
new BufferedReader(
new FileReader("[Link]"));
Read Line
---
# FileReader vs BufferedReader
| FileReader | BufferedReader |
|-------------|----------------|
| Reads character by character | Reads line by line |
| Slower | Faster |
| No Buffer | Uses Buffer |
---
# 4. FileWriter
4
Used to **write character data** into a file.
Example
```java id="2n5q8v"
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Java");
[Link]();
Output
---
# Append Mode
use
```java id="7v3m2p"
FileWriter fw =
new FileWriter("[Link]", true);
5. BufferedWriter
Used to write text efficiently using a buffer.
Advantages
Example
[Link]("Welcome");
5
[Link]();
---
# FileWriter vs BufferedWriter
| FileWriter | BufferedWriter |
|-------------|----------------|
| Direct Writing | Buffered Writing |
| Slower | Faster |
| No Buffer | Uses Buffer |
---
# Reading a File
Example
```java id="4q8m7v"
BufferedReader br =
new BufferedReader(
new FileReader("[Link]"));
String line;
[Link](line);
[Link]();
Writing a File
Example
[Link]("Core Java");
[Link]();
6
---
# Closing Files
Example
```java id="1m4v8q"
[Link]();
[Link]();
[Link]();
[Link]();
Reason
Example
FileReader fr =
new FileReader("[Link]");
catch(IOException e){
[Link](e);
7
`IOException` is a **Checked Exception**.
---
| Method | Description |
|----------|-------------|
| exists() | Checks file existence |
| createNewFile() | Creates file |
| delete() | Deletes file |
| mkdir() | Creates directory |
| getName() | File name |
| getPath() | File path |
| length() | File size |
---
### MCQ 1
A) [Link]
B) [Link]
C) [Link]
D) [Link]
**Answer:** C
---
### MCQ 2
A) FileReader
B) FileWriter
C) File
D) BufferedReader
8
**Answer:** C
---
### MCQ 3
A) FileWriter
B) FileReader
C) BufferedWriter
D) Scanner
**Answer:** B
---
### MCQ 4
A) FileReader
B) BufferedReader
C) FileWriter
D) File
**Answer:** C
---
### MCQ 5
A) FileReader
B) BufferedReader
C) Scanner
D) File
9
**Answer:** B
---
### MCQ 6
A) FileWriter
B) BufferedWriter
C) FileReader
D) Scanner
**Answer:** B
---
### MCQ 7
A) newFile()
B) create()
C) createNewFile()
D) makeFile()
**Answer:** C
---
### MCQ 8
A) check()
B) exists()
C) isFile()
D) available()
10
**Answer:** B
---
### MCQ 9
A) remove()
B) erase()
C) delete()
D) clear()
**Answer:** C
---
### MCQ 10
A) fileName()
B) getFile()
C) getName()
D) name()
**Answer:** C
---
### MCQ 11
A) size()
B) getSize()
C) length()
D) bytes()
11
**Answer:** C
---
### MCQ 12
A) ArithmeticException
B) IOException
C) NullPointerException
D) SQLException
**Answer:** B
---
### MCQ 13
A) FileReader
B) BufferedReader
C) Scanner
D) File
**Answer:** B
---
### MCQ 14
A) FileWriter
B) BufferedWriter
C) File
D) Scanner
12
**Answer:** B
---
### MCQ 15
**Answer:** C
---
---
✔ Always call
```java id="7r3m8q"
close();
13
Memory Tricks
Remember
Represents File
---
Remember
```text id="6m7q1k"
FileReader
Read Characters
Remember
Read Lines
Fast
---
Remember
```text id="5v8m3q"
FileWriter
14
Write Characters
Remember
Write Fast
---
Remember
```text id="4w6n2m"
[Link]
File Handling
One-Minute Revision
✔ File Handling is used to store data permanently.
15
✔ File handling commonly throws IOException.
Class Purpose
16
CORE JAVA FOR TCS IPA – Chapter 24: Java 8
Features (Detailed Notes + MCQs)
Introduction to Java 8
Java 8 (released in 2014) is one of the most important versions of Java.
• More concise
• Faster
• Functional
• Easy to read
Lambda Expression
Functional Interface
Stream API
Method Reference
Optional Class
Default Methods
---
1
# 1. Lambda Expression
**Syntax**
```java id="7n3v5m"
(parameters) -> expression
Example
Without Lambda
[Link]("Hello");
};
With Lambda
```java id="9m6q2v"
Runnable r = () -> [Link]("Hello");
Advantages
✔ Less Code
✔ Better Readability
✔ Functional Programming
2
Lambda Expression Components
```text id="8w4r1p" (parameters)
->
Expression / Body
Example
```java id="3k7m8q"
(a, b) -> a + b
2. Functional Interface
One of the most important TCS MCQs.
✔ Default Methods
✔ Static Methods
Example
interface Test {
void display();
3
Valid
---
```java id="6q8m1k"
@FunctionalInterface
interface Test {
void show();
void display();
Compile-Time Error
Reason
@FunctionalInterface Annotation
Used to indicate that an interface is a Functional Interface.
Example
interface Demo {
void test();
4
---
Package
```java id="9k4r6v"
[Link]
Examples
• Predicate
• Function
• Consumer
• Supplier
3. Stream API
The Stream API is used to process collections efficiently.
It supports
✔ Filtering
✔ Sorting
✔ Mapping
✔ Counting
✔ Reducing
Example
[Link]()
.forEach([Link]::println);
5
Output
```text id="5v8k2r"
3
Advantages
✔ Less Code
✔ Better Performance
✔ Parallel Processing
4. Method Reference
A Method Reference is a shorter form of a Lambda Expression.
Syntax
6
Example
Lambda
```java id="2n7k4p"
[Link](x -> [Link](x));
Method Reference
Advantages
✔ Cleaner Code
✔ Easy to Read
---
```java id="3m8v2k"
ClassName::staticMethod
---
3. Constructor Reference
```java id="4k9m6r"
ClassName::new
5. Optional Class
Introduced to avoid
7
```text id="9x2r5v" NullPointerException
Package
```java id="1m7k4q"
[Link]
Example
[Link]([Link]());
Advantages
✔ Safer Code
---
| Method | Purpose |
|----------|----------|
| of() | Creates Optional |
| ofNullable() | Allows null |
| isPresent() | Checks value |
| get() | Returns value |
| orElse() | Default value |
---
# 6. Default Methods
Example
```java id="2q8n5m"
interface Test {
[Link]("Default");
8
}
Purpose
Example
[Link]("Hello");
Called using
```java id="4v9k1q"
[Link]();
Java 8 vs Java 7
Java 7 Java 8
9
Java 7 Java 8
A) Java 6
B) Java 7
C) Java 8
D) Java 9
Answer: C
MCQ 2
A) No abstract methods
Answer: B
MCQ 3
A) @Override
10
B) @FunctionalInterface
C) @Deprecated
D) @SafeVarargs
Answer: B
MCQ 4
A) Stream API
B) Lambda Expression
C) Optional
D) Method Reference
Answer: B
MCQ 5
A) JDBC
B) Stream API
C) Reflection API
D) File API
Answer: B
MCQ 6
A) ::
11
B) ->
C) ::
D) =>
Answer: A
MCQ 7
A) Optional
B) Stream
C) String
D) Integer
Answer: A
MCQ 8
A) [Link]
B) [Link]
C) [Link]
D) [Link]
Answer: B
MCQ 9
A) Abstract
12
B) Default
C) Final
D) Protected
Answer: B
MCQ 10
A) Classes
B) Interfaces
C) Packages
D) Arrays
Answer: B
MCQ 11
A) ::
B) ->
C) =>
D) ==>
Answer: B
MCQ 12
13
B) Functional Interface can have only one abstract method.
Answer: B
MCQ 13
A) sorted()
B) distinct()
C) count()
D) filter()
Answer: B
MCQ 14
A) map()
B) filter()
C) count()
D) collect()
Answer: B
MCQ 15
14
B) Optional helps avoid NullPointerException.
Answer: D
✔ Lambda → ->
✔ Method Reference → ::
Memory Tricks
Remember
->
Anonymous Function
15
---
Remember
```text id="2n6k8r"
Functional Interface
Exactly One
Abstract Method
Remember
Annotation
---
Remember
```text id="4r8q5n"
Method Reference
::
Remember
Avoid
NullPointerException
16
---
Remember
```text id="1p6v4q"
Stream API
Process Collections
One-Minute Revision
✔ Java 8 introduced Lambda Expressions, Functional Interfaces, Stream API, Method References,
Optional, Default Methods, and Static Methods in Interfaces.
Feature Purpose
@FunctionalInterface Annotation
Method Reference ::
17
Feature Purpose
18
CORE JAVA FOR TCS IPA – Chapter 25: Important
Output-Based Questions (Detailed Notes + MCQs)
Introduction
Output-based questions are among the most frequently asked questions in the TCS IPA Java test.
• Operators
• Strings
• StringBuilder
• Increment/Decrement
• Loops
• Arrays
• Method Calls
• Object References
• Wrapper Classes
Tip: Never guess the output. Execute the code step by step mentally.
int x = 5;
[Link](x++);
[Link](x);
Output
1
Explanation
Memory Trick
Post Increment
Use
Increase
int x = 5;
[Link](++x);
[Link](x);
Output
Explanation
First increment,
Memory Trick
2
Pre Increment
Increase
Use
3. Combined Example
int x = 5;
[Link](x++);
[Link](++x);
Output
Explanation
Initially
x = 5
First line
Print 5
x becomes 6
Second line
3
Increment to 7
Print 7
4. String is Immutable
Example
String s = "Java";
[Link](" Programming");
[Link](s);
Output
Java
Explanation
Correct Way
s = [Link](" Programming");
[Link](s);
Output
Java Programming
4
5. StringBuilder is Mutable
Example
[Link](" Programming");
[Link](sb);
Output
Java Programming
Explanation
6. StringBuffer Example
[Link](" World");
[Link](sb);
Output
Hello World
7. Equality (==)
Example
String s1 = "Java";
5
String s2 = "Java";
[Link](s1 == s2);
Output
true
Reason
Both refer to the same object in the String Constant Pool (SCP).
8. equals()
Example
[Link]([Link](s2));
Output
true
Reason
[Link](s1 == s2);
6
Output
false
Reason
[Link]([Link]);
Output
Remember
Arrays use
length
NOT
length()
String s = "Java";
[Link]([Link]());
Output
7
4
Remember
Strings use
length()
12. charAt()
String s = "Java";
[Link]([Link](2));
Output
13. substring()
String s = "Programming";
[Link]([Link](3,7));
Output
gram
8
14. [Link]()
[Link]([Link]("100")+20);
Output
120
[Link](10+20+"Java");
Output
30Java
[Link]("Java"+10+20);
Output
Java1020
Output
true
9
17. Short-Circuit
int x = 5;
[Link](x);
Output
false
Reason
switch(day){
case "MON":
[Link]("Monday");
Output
Monday
(Java 7 onwards)
10
19. Default Value
class Test{
int x;
[Link](t.x);
Output
int x;
[Link](x);
Output
Compile-Time Error
11
Frequently Asked TCS MCQs
MCQ 1
Output
int x=5;
[Link](x++);
A) 5
B) 6
C) Error
D) 4
Answer: A
MCQ 2
Output
int x=5;
[Link](++x);
A) 5
B) 6
C) 7
D) Error
Answer: B
MCQ 3
Output
12
String s="Java";
[Link](" Programming");
[Link](s);
A) Java
B) Java Programming
C) Programming
D) Error
Answer: A
MCQ 4
Output
[Link](" Programming");
[Link](sb);
A) Java
B) Java Programming
C) Programming
D) Error
Answer: B
MCQ 5
A) StringBuilder
13
B) StringBuffer
C) String
D) ArrayList
Answer: C
MCQ 6
A) String
B) StringBuilder
C) Integer
D) Character
Answer: B
MCQ 7
A) ==
B) equals()
C) compare()
D) compareTo()
Answer: B
MCQ 8
Arrays use
A) length()
14
B) size()
C) length
D) count()
Answer: C
MCQ 9
Strings use
A) length
B) size()
C) length()
D) count()
Answer: C
MCQ 10
Output
[Link](10+20+"Java");
A) Java30
B) 30Java
C) 1020Java
D) Error
Answer: B
MCQ 11
Output
15
[Link]("Java"+10+20);
A) Java30
B) Java1020
C) 30Java
D) Error
Answer: B
MCQ 12
Output
[Link]([Link]("50")+50);
A) 5050
B) 100
C) Error
D) 50
Answer: B
MCQ 13
Local variables
C) Are always 0
Answer: B
16
MCQ 14
Instance variables
C) Must be initialized
D) Cannot be initialized
Answer: B
MCQ 15
A) StringBuilder is immutable.
B) String is mutable.
C) StringBuilder is mutable.
D) StringBuffer is immutable.
Answer: C
17
✔ False — == compares references.
Memory Tricks
Remember
x++
Use
Increase
++x
Increase
Use
String
Immutable
18
StringBuilder
Mutable
==
Reference
equals()
Content
Array
length
String
length()
One-Minute Revision
✔ x++ → Use first, then increment.
19
✔ ++x → Increment first, then use.
✔ String is immutable.
✔ == compares references.
Topic Remember
String Immutable
StringBuilder Mutable
== Reference Comparison
Array length
String length()
20
CORE JAVA FOR TCS IPA – Chapter 26: Frequently
Asked Java Exceptions (Detailed Notes + MCQs)
What is an Exception?
An Exception is an unexpected event that occurs during the execution of a program and disrupts its
normal flow.
For the TCS IPA Exam, these exceptions are asked repeatedly:
• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException
• ClassCastException
• NumberFormatException
• IOException
• SQLException
Exception Hierarchy
```text id="2m8q4v" Object
Throwable
Exception
RuntimeException
Memory Trick
```text id="8k3r1m"
Throwable
1
↓
Exception
RuntimeException
Types of Exceptions
Java exceptions are divided into two categories.
Checked Exceptions
✔ Checked during compilation.
Examples
• IOException
• SQLException
• FileNotFoundException
Unchecked Exceptions
✔ Occur during runtime.
Examples
• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException
• NumberFormatException
• ClassCastException
1. ArithmeticException
Occurs during illegal arithmetic operations.
2
✔ Divide by zero.
Example
[Link](a / 0);
Output
```text id="4p7x1q"
ArithmeticException
Reason
2. NullPointerException
Occurs when a null reference is used to access an object.
Example
[Link]([Link]());
Output
```text id="9m4q2k"
NullPointerException
Reason
3. ArrayIndexOutOfBoundsException
Occurs when an invalid array index is accessed.
3
Example
[Link](arr[5]);
Output
```text id="1k5r8q"
ArrayIndexOutOfBoundsException
Reason
4. ClassCastException
Occurs when an object is cast to an incompatible type.
Example
Integer x = (Integer)obj;
Output
```text id="2v7m1k"
ClassCastException
Reason
5. NumberFormatException
Occurs when converting an invalid string into a number.
Example
4
```java id="7m2p4q" [Link]("ABC");
Output
```text id="9r6k3v"
NumberFormatException
Reason
6. IOException
A Checked Exception.
Examples
Example
---
# 7. SQLException
A **Checked Exception**.
Examples
5
Example
```java id="6q1v8k"
Connection con = [Link](...);
SQLException is thrown.
Compile-Time Runtime
IOException ArithmeticException
SQLException NullPointerException
FileNotFoundException NumberFormatException
6
How to Handle Exceptions
Example
int a = 10 / 0;
catch(ArithmeticException e){
Output
```text id="5k2m9r"
Cannot divide by zero
A) IOException
B) NullPointerException
C) ArithmeticException
D) SQLException
Answer: C
7
MCQ 2
A) ArithmeticException
B) NumberFormatException
C) NullPointerException
D) IOException
Answer: C
MCQ 3
A) ClassCastException
B) ArrayIndexOutOfBoundsException
C) IOException
D) SQLException
Answer: B
MCQ 4
A) NumberFormatException
B) ClassCastException
C) ArithmeticException
D) IOException
Answer: B
8
MCQ 5
A) IOException
B) NumberFormatException
C) ArithmeticException
D) SQLException
Answer: B
MCQ 6
A) SQLException
B) IOException
C) ArithmeticException
D) NullPointerException
Answer: B
MCQ 7
A) IOException
B) ArithmeticException
C) SQLException
D) NumberFormatException
Answer: C
9
MCQ 8
A) ArithmeticException
B) NullPointerException
C) IOException
D) ClassCastException
Answer: C
MCQ 9
A) IOException
B) SQLException
C) NumberFormatException
D) FileNotFoundException
Answer: C
MCQ 10
A) IOException is unchecked.
B) SQLException is unchecked.
C) ArithmeticException is checked.
D) IOException is checked.
Answer: D
10
MCQ 11
A) NumberFormatException
B) IOException
C) NullPointerException
D) ArithmeticException
Answer: B
MCQ 12
Integer i = (Integer)obj;
A) NullPointerException
B) ArithmeticException
C) ClassCastException
D) IOException
**Answer:** C
---
### MCQ 13
A) Exception
B) Object
C) Throwable
D) RuntimeException
11
**Answer:** C
---
### MCQ 14
**Answer:** C
---
### MCQ 15
A) SQLException
B) IOException
C) ArithmeticException
D) ClassCastException
**Answer:** A
---
✔ It is a **Checked Exception**.
---
12
✔ `NumberFormatException` → Invalid String to Number Conversion.
---
---
# Memory Tricks
Remember
```text id="7p3m9k"
ArithmeticException
Divide by Zero
Null Object
---
```text id="1m4k7v"
ArrayIndexOutOfBoundsException
Wrong Index
13
Wrong Casting
---
```text id="4r6m8p"
NumberFormatException
Invalid Number
File Handling
---
```text id="2n8m5r"
SQLException
Database
One-Minute Revision
✔ ArithmeticException → Divide by zero.
14
✔ SQLException → Database errors (Checked).
Exception Cause
15
CORE JAVA FOR TCS IPA – Chapter 27: Java
Keywords (Must Memorize) – Detailed Notes +
MCQs
They cannot be used as identifiers (such as variable names, method names, or class names).
Example (Invalid)
Output
Compile-Time Error
this
super
static
final
abstract
synchronized
volatile
transient
1
native
strictfp
instanceof
1. this Keyword
this refers to the current object.
Uses
✔ Current object
Example
class Student{
int age;
Student(int age){
[Link] = age;
2. super Keyword
super refers to the parent class object.
2
Uses
Example
class Animal{
void sound(){
[Link]("Animal");
void display(){
[Link]();
this vs super
this super
3
3. static Keyword
Belongs to the class, not objects.
Uses
✔ Static Variables
✔ Static Methods
✔ Static Block
Example
class Test{
Memory
Class
Static
4. final Keyword
Used to restrict modification.
4
Example
5. abstract Keyword
Used to achieve abstraction.
✔ Classes
✔ Methods
Example
6. synchronized Keyword
Used in Multithreading.
Purpose
Example
5
Advantages
✔ Thread Safety
7. volatile Keyword
Used with variables.
Purpose
Ensures that every thread reads the latest value of the variable from main memory.
Example
Important
Used in multithreading.
8. transient Keyword
Used during Serialization.
Example
Memory Trick
transient
Temporary
6
↓
Not Saved
9. native Keyword
Indicates that a method is implemented in a language other than Java (usually C/C++).
Example
Example
Example
7
String s = "Java";
Output
true
Example
Output
true
Summary Table
Keyword Purpose
abstract Abstraction
8
Frequently Asked TCS MCQs
MCQ 1
A) Parent Class
B) Current Object
C) Current Package
D) Interface
Answer: B
MCQ 2
A) Current Class
B) Parent Class
C) Current Method
D) Interface
Answer: B
MCQ 3
A) final
B) static
C) this
D) super
9
Answer: B
MCQ 4
A) static
B) final
C) abstract
D) native
Answer: B
MCQ 5
A) interface
B) abstract
C) static
D) final
Answer: B
MCQ 6
A) volatile
B) synchronized
C) native
D) transient
10
Answer: B
MCQ 7
A) final
B) volatile
C) synchronized
D) native
Answer: B
MCQ 8
A) native
B) transient
C) volatile
D) strictfp
Answer: B
MCQ 9
A) transient
B) strictfp
C) native
D) synchronized
11
Answer: C
MCQ 10
A) final
B) volatile
C) strictfp
D) native
Answer: C
MCQ 11
A) typeof
B) instanceof
C) is
D) classof
Answer: B
MCQ 12
12
Answer: C
MCQ 13
A) super
B) this
C) Both A and B
D) static
Answer: C
MCQ 14
A) transient
B) native
C) strictfp
D) volatile
Answer: A
MCQ 15
13
Answer: C
Memory Tricks
Remember
this
Current Object
super
14
Parent Object
static
Class
final
Cannot Change
abstract
Incomplete
synchronized
Thread Safety
volatile
Latest Value
15
transient
Not Serialized
native
C / C++
strictfp
Floating Point
instanceof
Type Check
One-Minute Revision
✔ this → Current Object.
16
✔ synchronized → Thread safety.
Keyword Remember
17
• strictfp ensures → Floating-Point Consistency
• instanceof checks → Object Type
18
CORE JAVA FOR TCS IPA – Chapter 28: Frequently
Asked Java Differences (Detailed Notes + MCQs)
Introduction
Difference-based MCQs are among the most frequently asked questions in the TCS IPA Java Exam.
Full Form Java Development Kit Java Runtime Environment Java Virtual Machine
Memory Trick
JRE
JVM
1
**Most Expected MCQ**
✅ **JDK**
---
# 2. String vs StringBuilder
| String | StringBuilder |
|----------|---------------|
| Immutable | Mutable |
| Slower | Faster |
| Thread Safe | Not Thread Safe |
| Creates New Object | Modifies Existing Object |
Example
```java id="2r8v5n"
String s = "Java";
[Link](" Programming");
[Link](s);
Output
---
```java id="4p7x2m"
StringBuilder sb = new StringBuilder("Java");
[Link](" Programming");
[Link](sb);
Output
2
---
# 3. StringBuffer vs StringBuilder
| StringBuffer | StringBuilder |
|---------------|---------------|
| Thread Safe | Not Thread Safe |
| Synchronized | Not Synchronized |
| Slower | Faster |
| Mutable | Mutable |
```text id="5n2q7v"
Buffer
Safe
Fast
---
# 4. ArrayList vs LinkedList
| ArrayList | LinkedList |
|------------|------------|
| Dynamic Array | Doubly Linked List |
| Fast Random Access | Slow Random Access |
| Slow Insertion | Fast Insertion |
| Less Memory | More Memory |
✅ **ArrayList**
---
3
# 5. HashMap vs Hashtable
| HashMap | Hashtable |
|----------|-----------|
| One Null Key | No Null Key |
| Many Null Values | No Null Values |
| Not Synchronized | Synchronized |
| Faster | Slower |
```text id="9v5m1q"
HashMap
1 Null Key
No Null
---
# 6. HashSet vs TreeSet
| HashSet | TreeSet |
|----------|---------|
| Unordered | Sorted |
| Uses HashMap | Uses Tree Structure |
| Faster | Slower |
| One Null Allowed | No Null Allowed |
✅ **TreeSet**
---
4
# 7. == vs equals()
| == | equals() |
|----|-----------|
| Reference Comparison | Content Comparison |
| Compares Memory Address | Compares Object Data |
| Used with Objects | Overridden by Many Classes |
Example
```java id="4n8q5m"
String s1 = new String("Java");
[Link](s1 == s2);
Output
---
```java id="6m3r8q"
[Link]([Link](s2));
Output
---
# 8. throw vs throws
| throw | throws |
|--------|---------|
| Throws an exception | Declares exceptions |
| Used Inside Method | Used in Method Declaration |
| Throws One Exception | Declares One or More Exceptions |
Example
5
```java id="7x5m2p"
throw new ArithmeticException();
```text id="1n6q4m"
throw
Throw
Declare
---
| Overloading | Overriding |
|--------------|------------|
| Same Method Name | Same Method Signature |
| Different Parameters | Same Parameters |
| Compile-Time Polymorphism | Run-Time Polymorphism |
| No Inheritance Required | Inheritance Required |
| Compiler Decides | JVM Decides |
```text id="2m7r5q"
Overloading
6
Compile-Time
Run-Time
---
✅ **Interface**
---
## List vs Set
| List | Set |
|------|-----|
| Duplicates Allowed | No Duplicates |
| Ordered | Usually Unordered |
---
| Checked | Unchecked |
|----------|-----------|
| Compile-Time | Runtime |
| IOException | ArithmeticException |
7
| SQLException | NullPointerException |
---
## start() vs run()
| start() | run() |
|----------|-------|
| Creates New Thread | Normal Method |
| Calls run() Internally | No New Thread |
---
## length vs length()
| length | length() |
|----------|----------|
| Arrays | String |
---
---
### MCQ 1
A) JVM
B) JRE
C) JDK
D) Bytecode
**Answer:** C
---
### MCQ 2
8
Which class is immutable?
A) StringBuilder
B) StringBuffer
C) String
D) ArrayList
**Answer:** C
---
### MCQ 3
A) StringBuilder
B) StringBuffer
C) ArrayList
D) LinkedList
**Answer:** B
---
### MCQ 4
A) LinkedList
B) ArrayList
C) HashSet
D) TreeSet
**Answer:** B
---
### MCQ 5
9
HashMap allows
A) No Null Key
**Answer:** B
---
### MCQ 6
Hashtable allows
**Answer:** C
---
### MCQ 7
A) HashSet
B) TreeSet
C) LinkedHashSet
D) ArrayList
**Answer:** B
---
### MCQ 8
10
`==` compares
A) Content
B) Memory Reference
C) Length
D) Characters
**Answer:** B
---
### MCQ 9
`equals()` compares
A) References
B) Contents
C) Memory Address
D) Objects Only
**Answer:** B
---
### MCQ 10
A) throw
B) throws
C) catch
D) finally
**Answer:** B
---
### MCQ 11
11
Method Overloading is
A) Run-Time Polymorphism
B) Compile-Time Polymorphism
C) Dynamic Binding
D) Inheritance
**Answer:** B
---
### MCQ 12
Method Overriding is
A) Compile-Time Polymorphism
B) Run-Time Polymorphism
C) Constructor Overloading
D) Static Binding
**Answer:** B
---
### MCQ 13
A) Abstract Class
B) Interface
C) Final Class
D) Thread Class
**Answer:** B
---
### MCQ 14
12
Which statement is TRUE?
**Answer:** C
---
### MCQ 15
C) StringBuilder is immutable.
**Answer:** C
---
---
✔ **StringBuffer** is thread-safe.
---
13
---
---
# Memory Tricks
Remember
```text id="3r7m1k"
JDK
Compiler
Immutable
---
```text id="6m4k8r"
StringBuilder
Fast
1 Null Key
14
---
```text id="8q3v7m"
Hashtable
No Null
Compile-Time
---
```text id="5p1m6q"
Overriding
Run-Time
One-Minute Revision
✔ JDK → Development (Compiler Included)
✔ String → Immutable
15
✔ StringBuilder → Mutable & Fast
✔ HashSet → Unordered
✔ TreeSet → Sorted
✔ == → Reference Comparison
✔ Overloading → Compile-Time
✔ Overriding → Run-Time
Topic Remember
16
Topic Remember
17
CORE JAVA FOR TCS IPA – Chapter 29: Must Know
Java APIs (Detailed Notes + MCQs)
Introduction
Java provides a large number of built-in classes known as Java APIs (Application Programming
Interfaces).
For the TCS IPA Exam, you are not expected to memorize every API, but you must know the most
commonly used classes and their methods.
• Scanner
• Arrays
• Collections
• Math
• Character
• String
• StringBuilder
• ArrayList
• HashMap
• HashSet
• LinkedList
• Queue
• Stack
1. Scanner Class
Package
Example
```java id="6m1v8q"
Scanner sc = new Scanner([Link]);
1
Important Methods
Method Purpose
Example
**TCS MCQ**
✅ **nextLine()**
---
# 2. Arrays Class
**Package**
```java id="9v5r2q"
[Link]
Important Methods
Method Purpose
2
Method Purpose
Example
[Link](arr);
Output
```text id="7n3q1p"
2 4 5
[Link]()
Example
[Link]([Link](arr,30));
Output
```text id="8p4v7r"
2
3. Collections Class
Package
3
Provides utility methods for collections.
## Important Methods
| Method | Purpose |
|----------|---------|
| sort() | Sort List |
| reverse() | Reverse List |
| max() | Maximum Element |
| min() | Minimum Element |
| shuffle() | Random Order |
Example
```java id="1v7k4m"
[Link](list);
4. Math Class
Package
Automatically imported.
## Important Methods
| Method | Purpose |
|----------|---------|
| abs() | Absolute value |
| sqrt() | Square root |
| pow() | Power |
| max() | Maximum |
| min() | Minimum |
| random() | Random number |
| ceil() | Round up |
| floor() | Round down |
Example
```java id="9m1k7v"
[Link]([Link](25));
4
Output
---
# 5. Character Class
**Package**
```java id="2n8v5m"
[Link]
Important Methods
Method Purpose
Example
Output
```text id="4p8v1r"
true
6. String Class
Most important Java API.
5
Important Methods
Method Purpose
substring() Substring
toUpperCase() Uppercase
toLowerCase() Lowercase
7. StringBuilder Class
Mutable string class.
Important Methods
Method Purpose
6
Method Purpose
length() Length
Example
[Link](" Programming");
---
# 8. ArrayList Class
## Important Methods
| Method | Purpose |
|----------|---------|
| add() | Insert element |
| remove() | Delete element |
| get() | Access element |
| set() | Update element |
| contains() | Check existence |
| size() | Number of elements |
| clear() | Remove all |
| isEmpty() | Empty check |
Example
```java id="1m5q8v"
[Link](10);
[Link](0);
[Link](0);
7
9. HashMap Class
Stores key-value pairs.
Important Methods
Method Purpose
Example
[Link](1);
---
## Important Methods
| Method | Purpose |
|----------|---------|
| add() | Add element |
| remove() | Remove element |
| contains() | Check existence |
| size() | Number of elements |
| clear() | Remove all |
8
---
## Important Methods
| Method | Purpose |
|----------|---------|
| add() | Insert |
| remove() | Delete |
| get() | Access |
| addFirst() | Insert at beginning |
| addLast() | Insert at end |
| removeFirst() | Remove first |
| removeLast() | Remove last |
---
FIFO structure.
## Important Methods
| Method | Purpose |
|----------|---------|
| offer() | Insert |
| add() | Insert |
| peek() | View first element |
| poll() | Remove first element |
| remove() | Remove first |
Example
```java id="5r9m3q"
[Link](10);
[Link]();
[Link]();
9
13. Stack Class
LIFO structure.
Important Methods
Method Purpose
push() Insert
Example
[Link]();
[Link]();
---
---
10
# Frequently Asked TCS MCQs
### MCQ 1
A) next()
B) nextLine()
C) nextInt()
D) nextDouble()
**Answer:** C
---
### MCQ 2
A) [Link]()
B) [Link]()
C) sort()
D) [Link]()
**Answer:** B
---
### MCQ 3
A) [Link]()
B) [Link]()
C) [Link]()
D) [Link]()
**Answer:** B
11
---
### MCQ 4
A) [Link]()
B) [Link]()
C) [Link]()
D) [Link]()
**Answer:** B
---
### MCQ 5
A) Arrays
B) Math
C) Character
D) String
**Answer:** B
---
### MCQ 6
A) isDigit()
B) isNumber()
C) digit()
D) isInteger()
**Answer:** A
12
---
### MCQ 7
A) concat()
B) append()
C) add()
D) insert()
**Answer:** B
---
### MCQ 8
A) add()
B) insert()
C) put()
D) set()
**Answer:** C
---
### MCQ 9
A) values()
B) keys()
C) keySet()
D) getKeys()
**Answer:** C
13
---
### MCQ 10
A) keySet()
B) values()
C) valueSet()
D) getValues()
**Answer:** B
---
### MCQ 11
A) peek()
B) poll()
C) push()
D) offer()
**Answer:** B
---
### MCQ 12
A) push()
B) peek()
C) pop()
D) remove()
**Answer:** C
14
---
### MCQ 13
A) search()
B) contains()
C) exists()
D) find()
**Answer:** B
---
### MCQ 14
**Answer:** B
---
### MCQ 15
**Answer:** D
15
---
---
---
---
---
# Memory Tricks
Remember
```text id="7m4q1p"
Scanner
nextInt()
Input
16
```text id="3k9v5r" Arrays
sort()
---
```text id="1n8m2q"
Collections
sort(List)
sqrt()
pow()
---
```text id="4p5k8n"
Character
isDigit()
isLetter()
put()
get()
17
---
```text id="2r8k3p"
Queue
peek()
poll()
push()
pop() ```
One-Minute Revision
✔ Scanner → nextInt() , next() , nextLine()
18
✔ LinkedList → addFirst() , removeFirst()
19