USER DEFINED METHODS/FUNCTIONS :
• Parameters at the function/method line are called formal parameters
• Parameters in the function call statement are called actual parameters
• Number of Actual and formal parameters must be same.
• ACTUAL PARAMETERS ARE ALSO CALLED ARGUMENT VARIABLES.
• Only ONE value can be returned at a time.
• void means the function does not return any value.
• Function Signature = Function name + parameter list(number and datatype of
parameters)
• Function Prototype = Function Signature + return type
• Functions which DO NOT CHANGE THE STATE OF THE OBJECT IS CALLED PURE
FUNCTIONS
• Methods which CHANGE THE STATE OF THE OBJECT are called IMPURE FUNCTIONS
• Methods can be called in two ways 1) call by value 2) Call by reference
• PRIMITIVE DATATYPE VALUES ARE PASSED IN CALL BY VALUE
• Call by value is when you send values to other functions
• Call by reference means when you send an object/ memory address
to other functions.
• Any number of user defined functions can be written in a program
• Many functions with same name is called function overloading.
• Method overloading is OOP feature of Polymorphism
• Functions called or invoked with help of class name are called static functions. (All
Math class functions and Character class functions)
• Functions called or invoked with help of object names are called non-static functions.
(All String class functions and Scanner class functions)
• The keyword return returns any kind of primitive datatype kind of value.
• The datatype of the returned value and datatype mentioned in the function
definition line must be same.
• Function definition line is called Function/Method Header.
• Advantages/Uses of User Defined Functions :
(a) Improve code readability: User-defined functions make code more readable.
(b) Improve code reusability: User-defined functions can be reused.
(c) User defined functions help in easier testing of the code and error detection.
Assertion (A): User-defined methods can have the same name as other methods in the
same class.
Reasoning (R): Java allows method overloading, where methods can have the same name
but different parameter lists
1. Both A and R are true, and R is the correct explanation of A.
2. Both A and R are true, but R is not the correct explanation of A.
3. A is true, but R is false.
4. A is false, but R is true.
Assertion (A): A user-defined method in Java must always have a return type.
Reasoning (R): The void keyword is used as a return type to indicate that the method does
not return any value.
1. Both A and R are true, and R is the correct explanation of A.
2. Both A and R are true, but R is not the correct explanation of A.
3. A is true, but R is false.
4. A is false, but R is true.
LOOPING CONSTRUCTS/STATEMENTS :
• 3 looping constructs : for , while and do while (Keywords)
• for and while are called entry controlled loop
• do while is called exit controlled loop since the test expression is written after the
body of the loop
• for loop is used when number of iterations are known
• while and do while are used when number of iterations are unknown.
• break keyword stops the iteration whereas continue skips the current iteration
• break , continue and return are called JUMP statements/keywords.
• Null Loop is called Time Delay Loop.
• Infinite loop can occur when test expression is missing/framed incorrectly.
• Infinite loop can occur when update expression is missing/framed incorrectly.
• There can be multiple initializations and update expression but only ONE test
expression/condition testing expression .
for(int i=1, j = 1; i<=3; i++ , j = j+2)
System.out.println(i + " " + j);
Assertion (A): A for loop is best suited when the number of iterations is known
beforehand.
Reasoning (R): A for loop provides initialization, condition checking, and
increment/decrement in a single construct, making it efficient for definite iteration.
1. Both A and R are true, and R is the correct explanation of A.
2. Both A and R are true, but R is not the correct explanation of A.
3. A is true, but R is false.
4. A is false, but R is true.
Assertion (A): The while loop is an entry-controlled loop.
Reasoning (R): The condition in a while loop is checked before the loop body is executed.
1. Both A and R are true, and R is the correct explanation of A.
2. Both A and R are true, but R is not the correct explanation of A.
3. A is true, but R is false.
4. A is false, but R is true.
Assertion (A): The continue statement skips the rest of the current iteration and proceeds
with the next iteration of the loop.
Reasoning (R): The continue statement is used to bypass certain code in the loop for the
current iteration and resume at the loop's next iteration
1. Both A and R are true, and R is the correct explanation of A.
2. Both A and R are true, but R is not the correct explanation of A.
3. A is true, but R is false.
4. A is false, but R is true.
STRING LIBRARY
• String class functions are non-static i.e functions are accessed with help of String
object/variable.
• String objects are IMMUTABLE. (i.e once a String is created, its content cannot be
changed and if changed it will lead to the creation of a new String.)
Assertion (A): The String class in Java is immutable.
Reasoning (R): Once a String object is created, its value cannot be changed, and any
operation that modifies a String creates a new object.
1. Both A and R are true, and R is the correct explanation of A.
2. Both A and R are true, but R is not the correct explanation of A.
3. A is true, but R is false.
4. A is false, but R is true.
• replace(char,char) and replace(String,String) are overloaded functions
• substring(int) and substring(int,int) are overloaded functions.
• equals() checks for only equality where compareTo( ) not only checks for equality but
also tells which String value is bigger.
• compareTo( ) compares 2 String lexicographically i.e takes the UNICODE/ASCII codes
into consideration.
• charAt() and indexOf() are opposite functions i.e charAt( ) returns the character at a
certain position whereas indexOf( ) gives position/index of a certain character.
• concat( ) String function joins ONLY 2 String at a time and concatenation operator +
joins many Strings at a time.
• Character functions are STATIC functions.
• If toLowerCase( ) and toUpperCase( ) are accessed using String variable/object , it
works ONLY ON String values. If accessed with Character class, it works ONLY char
values.
• String values are in double quotes ( “ “) and char values are single quotes (‘ ‘).
• Unicode numbers for the letters A–Z are: 65 – 90
• Unicode numbers for the letters a – z are: 97 – 122
• Unicode numbers for space : 32
• Unicode numbers for the digits 0 – 9 is : 48 – 57
• “cheatsheet”. indexOf(‘S’) will give output as -1.
• “cheatSheet”.lastIndexOf(‘E’) will give output as -1.
• “cheatsheet”. charAt(20) OR “cheatSheet”.charAt(-3) will give RUNTIME ERROR
(StingIndexOutOfBoundsException / NegativeIndexOutOfBoundsException)
Method Description Return Type
charAt() Returns the character at char
the specified index
(position)
compareTo() Compares two strings int
lexicographically
compareToIgnoreCase() Compares two strings int
lexicographically,
ignoring case differences
concat() joins a string to the end String
of another string
endsWith() Checks whether a string boolean
ends with the specified
character(s)
equals() Compares two strings. boolean
Returns true if the strings
are equal, and false if not
equalsIgnoreCase() Compares two strings, boolean
ignoring case
considerations
indexOf() Returns the position of int
the first occurrence of
specified characters in
the input string
lastIndexOf() Returns the position of int
the last found occurrence
of specified characters in
the input string
length() Returns the length of a int
specified string
replace() Searches a string for a String
specified value, and
returns a new string
where the specified
values are replaced
startsWith() Checks whether a string boolean
starts with specified
characters
substring() Returns a new string String
which is the substring of
a specified string
toLowerCase() Converts a string to String
lower case letters
toString() Converts and returns the String
value in the form of
String
toUpperCase() Converts a string to String
upper case letters
trim() Removes whitespace String
from both ends of a
string
valueOf() Returns the string
representation of the
specified value
This method is used to determine if the given character
boolean isLetter(char ch) value (ch) is a letter. This method returns true if it is a
character ([A-Z], [a-z]), false otherwise
This method is used to determine whether the specified
boolean isDigit(char ch) char value(ch) is a digit or not. Here also, we can pass the
`ASCII value as an argument.
Determines whether the specified char value(ch) is white
boolean isWhitespace(char ch)
space. Whitespace includes space, tab, or newline
Determines whether the specified char value(ch) is
boolean isUpperCase(char ch)
uppercase or not
Determines whether the specified char value(ch) is
boolean isLowerCase(char ch)
lowercase or not.
This method is used to determine whether the specified
char value(ch) is a digit or not or Letter or not(both upper
boolean isLetterOrDigit(char ch)
and lower case). Here also, we can pass the `ASCII value as
an argument.
Returns the uppercase of the specified char value(ch). If
char toUpperCase(char ch) an ASCII value is passed, then the ASCII value of its
uppercase will be returned
Returns the lowercase of the specified char value(ch). If
char toLowerCase(char ch) an ASCII value is passed, then the ASCII value of its
lowercase will be returned
Here also, we can pass the `ASCII value as an argument means :
Character.isLetter(97); o/p : true
(we can give an ascii code to check if the number represents a letter or not)
Character.isWhiteSpace(39); o/p : false (39 is NOT the numerical representation of space).