Encapsulation and abstraction are both principles of object oriented programming.
Encapsulation binds the attributes and methods that work on data together in a single class.
On the other hand, abstraction refers to the act of representing only essential features and hiding the
complex background details.
Symbolic constants are the names given to fixed values in a program. These constants can be used to
replace the actual values by a name.
For e.g., final double max_marks = 100; Now the value of the variable max_marks cannot be changed in
the program. Any statement such as max_marks = 150; will result in an error.
When main() is declared satic it can only access static methods.
Type conversion in Java is the process of converting a variable from one data type to another.
Implicit Conversion (Widening): Done automatically when converting a smaller data type to a larger
one.
Example: int a = 5; double b = a;
Explicit Conversion (Narrowing): Done manually when converting a larger data type to a smaller one.
Example: double x = 8.6; int y = (int) x;
The new operator in Java is used to create objects from a class. It allocates memory and returns a
reference to the newly created object.
For example:
Student s = new Student();
This line creates an object s of class Student using the new operator.
It is important because without new, no object is created, and the class members cannot be accessed
through an instance.
User-defined Class Built-in Class
A user-defined class is a class which is created by A built-in class is a class provided by Java as a part
the user/programmer. of standard library.
It needs to be compiled and used in the program It can be imported and used in the program.
by the user itself.
It is used to defined custom objects and perform It is used for common tasks like input, output,
operations as required by that object. Math functions, String functions etc.
A class in Java defined the structure and behaviour (data and methods) that objects will have. It acts as a
blueprint.
An object, on the other hand, is a runtime instance of that class that actually holds the data and
performs actions.
The class cannot function alone in a running program — it must be used to create objects.
Similarly, an object cannot exist without a class. Thus, they work together: the class defined, and the
object implements.
In Java, break is used to exit a loop prematurely when a certain condition is met, while continue is used
to skip the current iteration and continue with the next one.
break example:
for(int i = 1; i <= 5; i++) {
if(i == 3) break;
[Link](i + " "); // Output: 1 2
}
continue example:
for(int i = 1; i <= 5; i++) {
if(i == 3) continue;
[Link](i + " "); // Output: 1 2 4 5
}
Actual vs Formal Parameters
Formal parameters are declared in the method definition and are used to receive values when the
method is called.
Pure vs Impure Methods
Java uses call-by-value, which means a copy of the actual value is passed to the method. Changes made
inside the method
affect only the copy, not the original variable.
This is true for primitive data types. For objects, the reference is passed by value, but the concept
remains call-by-value.
Function Overloading
When multiple methods have same name, the compiler identifies the correct methods to call based on
the method signature which makes the method unique. It includes the number of parameters or type of
parameters or both.
Ambiguous method invocation can be avoided by ensuring that each overloaded method has clearly
distinguishable parameter types, order, or count. Using different data types or number of arguments
avoids confusion for the compiler
during method resolution. The best approach is to design method signatures so that they are not reliant
on implicit type conversions, which often cause ambiguity.
Function Prototype
Char findGrade(double percentage)
Wrapper Classes
[Link]() is a static method that interprets the sequence of characters in a string as a number
and returns its int value. The constructor Integer() creates an object; toString() converts numbers to
strings; convert() is not a method in the Integer class.
Integer n = Integer parseInt("451"); assigns a primitive int (returned by parseInt()) to a reference of type
Integer. This triggers
autoboxing, wrapping the int into an Integer object. So the object is created indirectly, not directly by
parseInt().
Boxing vs Unboxing
Boxing is the process of converting a primitive data type, such as int, char, double, etc. into its
corresponding wrapper class object i.e., Integer, Character, Double.
Integer a = new Integer(100); creates an Integer wrapper object with value 100.
Unboxing is the process of converting a wrapper class object back into its primitive data type.
Autoboxing and Auto-unboxing
Converting a wrapper object to its primitive value Explanation: Unboxing is the process of extracting the
primitive value from its wrapper class object (e.g., converting Integer to int).
Integer a = new Integer(100); creates an Integer wrapper object with value 100.
int b = a; is an example of autounboxing, where the value from the wrapper object a is automatically
converted to the primitive int. [Link](b); prints the primitive value 100. Thus, the output is
100, not "100" (which would be a string), and no compilation error occurs.
Pure vs Impure method
Method which reverses a given number is:
(a) Impure method (b) Pure method (c) Constructor (d) Destructor
Invoking a method by passing the objects of a class is termed as:
(a) Call by reference (b) Call by value (c) Call by method (d) Call by constructor
Assertion (A): Static method can access static and instance variables.
Reason (R): Static variable can be accessed only by static method.
(a) Assertion and Reason both are correct. (b) Assertion is true and Reason is false. (c) Assertion is false
and Reason is true. (d) Assertion and Reason both are false.
Library Classes
The two advantages of library classes are:
(1) You can use pre-defined classes and methods instead of writing code from scratch.
(2) Since common functions are pre-written, it saves time and e 昀昀 rt and makes coding faster.
In Java, the method to find the length of a string is length() — it returns the number of characters in the
string.
The string s = "abcde" has length 5.
[Link]() - 2 →5 - 2 = 3
[Link](3) gives the character at index 3, which is 'd' (a=0, b=1, c=2, d=3, e=4)
[Link]('o', 'a') replaces all occurrences of 'o' with 'a' in the string "computer". "computer" has 'o' at
index 1 → it's replaced with 'a'. The resulting string is "campater".
valueOf(): This method returns the string representation of the argument. For e.g.,
int num = 100;
String str = [Link](num);
[Link](str); // Output: "100"
trim(): The trim() method of the String class removes the leading and trailing spaces from a string. It
does not remove spaces present in between the string. For e.g.,
String s = " Hello Java ";
[Link]([Link]()); // Output: "Hello Java”
[Link](char ch) checks whether the given character is an uppercase letter (A–Z).
Usage Example:
if ([Link]('H'))
[Link]("Capital Letter");
[Link](char ch) checks whether the given character is a lowercase letter (a–z).
Usage Example:
if ([Link]('m'))
[Link]("Small Letter");
compareTo() equals()
The compareTo() method compares the string The equals() method checks if two strings are
lexicographically. exactly equal.
It returns an integer value. It returns Boolean value.
It is used when sorting strings. It is used when checking equality between
strings.
For e.g., "apple".compareTo ("banana") returns a For e.g., "apple". equals("banana") returns false.
negative value.
Scope of Variables
A variable declared inside a method is a local variable. It exists only during the method's execution
cannot be accessed from other methods or outside the class.
A local variable is declared inside a method or block and can only be used within that method or block.
Method-level — since it exists only during the method’s execution and is not visible elsewhere
Static and Non-Static Variables
A static variable belongs to the class rather than any individual object. It is shared across all instances of
the class, meaning any change made to it by one object is reflected in all others.
Its scope is class-level, allowing it to be accessed using the class name and retaining a single copy in
memory.
Because static methods belong to the class, while non-static (instance) variables belong to objects.
Without an object, they can’t be accessed.
Static Methods Non-static Methods
Static methods can be called directly using the A non-static method requires an object of the
class name without creating an object. class to be called
Searching
Binary Search works only on sorted arrays and starts by comparing the target with the middle element,
reducing the search space by half with each step.
Binary search works by repeatedly dividing the array into halves and comparing the middle element
with the target value. If the array is unsorted, we cannot determine whether the target element lies in
the left or right half after comparing with the middle element, because the elements are not arranged in
any logical order. Therefore, binary search needs a sorted array to accurately decide which half to
search next. This technique makes the process fast and efficient.
In Java, arrays have fixed sizes. Trying to access an index outside the valid range (0 to length–1) causes
the JVM to throw a runtime exception, not auto-correction, resizing, or defaulting to index 0.
If index is out of range then the program throws an ArrayIndexOutOfBoundsException at runtime.
Declaration of array:
char c[ ] [ ] = new char [4][4];
Each char data type in java occupies 2 bytes of memory. Since the array has 4 rows and 4 columns, the
total memory consumed is: 2 × 4 × 4 = 32 bytes
Sorting
Selection Sort is a technique of sorting array element in ascending or descending order. In this
technique, the array is divided into a sorted and an unsorted part. In each pass, the smallest (or largest)
element from the unsorted part is selected and then swapped with the 昀椀 st element of the unsorted
part. This process is repeated until the entire array is
sorted.
// Menu driven program to check automorphic number and abundant number
import [Link].*;
class menu
{
public static void main()
{
int ch, n;
Scanner sc = new Scanner([Link]);
// accept number to perform task
according to the choice
[Link]("Enter a number: ");
n = [Link]();
// display menu options
[Link]("Enter choice to
check: \n 1. Automorphic Number \n 2.
Abundant Number");
ch = [Link]();
// Accept choice from the user
switch (ch)
{
case 1:
// Check automorphic number
int n_copy = n;
int sq = n * n;
int count = 0;
while(n != 0)
{
count++;
n = n/10;
}
int last_digit = (sq % (int) Math.
pow(10, count));
if (last_digit == n_copy)
[Link](n_copy + " is
an automorphic number");
else
[Link](n_copy + " is
not an automorphic number");
break;
case 2:
// Check abundant number
int sum = 0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
sum = sum + i;
}
if (sum > n)
[Link](n + " is an
abundant number");
else
[Link](n + " is not an
abundant number");
break;
default:
[Link]("Not a valid choice");
}
}
}