Java Notes
Java Notes
Java Features
Here we list the basic features that make Java a powerful,
object-oriented, and popular programming language.
1. Platform Independence
2. Object Oriented
3. Both Compiled and Interpreted
4. Java is Robust
5. JAVA Language Security Features
6. Java is Multithreaded
First Program:
Class Example
{
public static void main(String args[])
{
System.out.println(“Hello World”);
}
}
Explanation
L class Example{
2
This line uses the keyword class to declare new class
followed by the class name.
The entire class definition will be between the opening
curly brace ({ ) and the closing curly brace ( }).
L }
6 closing of the main method.
L }
7 closing of the class.
Variables:
Identifier
Keywords:
Literals
Operators
1. Binary Operator:
Assignment Operator:
Arithmetic Operator:
Relational Operator:
Logical Operator:
Assignment Operator:
Example:
Arithmetic Operator:
Logical Operator:
Logical Operators are primarily used in decision-making
and control flow, allowing multiple conditions to be
combined or negated.
o Logical AND (&&):
o Logical OR (||):
o Logical NOT (!):
2. Logical OR (||):
Returns true if at least one condition is true.
If both conditions are false, the result is false.
Example:
2. Unary Operator:
Syntax:
variable = (condition) ? expression1 : expression2
Example:
package First_Package;
public class New_class {
public static void main(String[] args) {
Operator Precedence:
When various operators are used within the same
statement a problem arises that which operation should
be performed first. To solve this problem there is the
concept of operator precedence.
Operator Precedence is a set of rules that defines the
priority for all the operators.
Operators with higher precedence are evaluated first
before those with lower precedence.
Example:
a= 4+5/10;
Output:
a=4.5
Associativity:
If we have operators which have same precedence in
the same statement, then we use associativity to figure
out which operation should be performed first.
associativity determines the order of evaluation.
Operators can be left-associative or right-associative.
Example:
a= 5*5/10
Output:
a=2.5
double -> float -> long -> int -> char -> short -
> byte
inti = (int)(10.0/2.0);
Flow of Control
1. Conditional Statements:
a) If…else:
It is commonly used when you have multiple conditions to
evaluate.
You can have n number of else if (){} statements in your
program, as per your requirement.
Syntax:
if (condition1)
{
// Lines of code
}
else if(condition2)
{
// Lines of code
}
………
else
{
// Lines of code
}
Example:
package First_Package;
public class First_Program {
int number=5;
if(number > 0)
{
System.out.println(number + " is positive");
}
else if (number < 0)
{
System.out.println(number + " is negative");
}
else
{
System.out.println("The Number is Zero");
}
}
}
if (condition)
{
if (condition)
{
//Lines of code
}
}
b) Switch-case:
Java has a shorthand for multiple if statement—the switch-
case statement.
Here is how we can write the above program using a
switch-case:
Syntax:
switch (x)
{
case 0:
// Lines of code
doSomething0();
break;
case 1:
// Lines of code
doSomething1();
break;
...
case n:
// Lines of code
doSomethingN();
break;
default:
doSomethingElse();
}
Example:
package First_Package;
public class First_Program {
}
}
}
2. Loops:
The purpose of loop statements is to execute Java
statements many times.
There are three types of loops in Java—
a) For,
b) While, and
c) Do-while.
a) For Loop:
Syntax:
Example:
b) While Loop:
The while loop is used to repeatedly execute a block of
statements until the specified condition is true.
Once the condition becomes false, the loop automatically
stops.
Syntax:
while (condition)
{
//code to be executed
Increment / decrement
}
Example:
c) Do-while Loop:
Syntax:
do{
//code to be executed
}while (condition);
Example:
for-each Loop:
The for-each loop in Java also called the enhanced for
loop which was introduced in Java 5.
It provides an alternative approach to traverse the array
or collection in Java.
The advantage of the for-each loop is that it makes the
code more readable.
The drawback of the enhanced for loop is that it cannot
traverse the elements in reverse order.
Here, you do not have the option to skip any element
because it does not work on an index basis.
Syntax:
package First_Package;
public class New_class {
}
}
3. Branching Mechanism:
a) break Statement:
The break statement is used to jump out of a loop.
In other words, break is used to exit from the loop or
switch case.
When break statement is encountered inside a loop, the
loop is immediately terminated and the Program control
moves to the next statement after the loop.
Syntax:
break;
Example:
package First_Package;
public class First_Program {
}
}
b) Continue Statement:
Continue statement is used to skip the current
iteration and continues with the next iteration in the
loop.
We can use Java continue statement in all types of loops
such as for loop, while loop and do-while loop.
Syntax:
continue;
Example:
package First_Package;
public class First_Program {
}
Okay, here are 10 MCQs for each major topic identified in your provided PDF.
1. Which logical operator returns true only if both conditions are true?
a) || (Logical OR)
b) ! (Logical NOT)
c) && (Logical AND)
d) ^ (Logical XOR - not explicitly listed but good distractor)
Answer: c)
2. If condition1 is false and condition2 is true, what is the result of condition1 ||
condition2?
a) true
b) false
c) 0
d) 1
Answer: a)
3. What is the purpose of the Logical NOT (!) operator?
a) To combine two boolean expressions.
b) To reverse the boolean value of an expression.
c) To check for inequality.
d) To perform bitwise negation.
Answer: b)
4. Which of the following is a unary operator?
a) + (addition)
b) && (logical AND)
c) ++ (increment)
d) ?: (ternary)
Answer: c)
5. If int x = 2; int y = ++x;, what are the values of x and y after execution?
a) x = 2, y = 2
b) x = 2, y = 3
c) x = 3, y = 2
d) x = 3, y = 3
Answer: d)
6. If int x = 2; int y = x++;, what are the values of x and y after execution?
a) x = 2, y = 2
b) x = 3, y = 2
c) x = 2, y = 3
d) x = 3, y = 3
Answer: b)
7. The bitwise logical NOT operator is represented by:
a) !
b) ~
c) NOT
d) ^
Answer: b)
8. The ternary operator (?:) consists of how many operands?
a) One
b) Two
c) Three
d) Four
Answer: c)
9. In the ternary expression variable = (condition) ? expression1 : expression2;, when is
expression2 executed?
a) When the condition is true.
b) When the condition is false.
c) Always executed, but its value is chosen based on condition.
d) Never, it's for comments.
Answer: b)
10. If boolean flag = true;, what is the output of System.out.println(!flag);?
a) true
b) false
c) 1
d) 0
Answer: b)
1. Which type of statements are used to control the flow of execution of a program based
on certain conditions?
a) Looping statements
b) Assignment statements
c) Control flow statements
d) Declaration statements
Answer: c)
2. Which of the following is a conditional statement provided by Java as listed in the
document?
a) For loop
b) While loop
c) Switch-case
d) Try-catch
Answer: c)
3. In an if-else if-else structure, which block is executed if none of the if or else if
conditions are true?
a) The first if block.
b) All else if blocks.
c) The final else block.
d) No block is executed.
Answer: c)
4. Can if...else conditions be nested in Java?
a) No, Java does not allow nested conditionals.
b) Yes, if...else conditions can be nested.
c) Only if can be nested, not else.
d) Only up to two levels of nesting.
Answer: b)
5. The switch-case statement is described as a shorthand for:
a) A single if statement.
b) Multiple independent if statements.
c) A for loop.
d) Multiple if-else if statements (a chain of if statements).
Answer: d)
6. What is the purpose of the break statement within a switch-case block?
a) To skip the current case and move to the next one.
b) To exit the switch-case block entirely.
c) To indicate the default case.
d) To repeat the current case.
Answer: b)
7. In a switch statement, if no case matches the value of the expression and a default
case is present, which block of code is executed?
a) The first case.
b) The last case.
c) The default case.
d) An error occurs.
Answer: c)
8. Consider the example: int day=3; switch(day) { ... case 3:
System.out.println("Wednesday"); break; ... }. What is the output?
a) Monday
b) Tuesday
c) Wednesday
d) Invalid day
Answer: c)
9. If a break statement is omitted from a case in a switch statement, what happens?
a) A compile-time error occurs.
b) Execution stops at that case.
c) Execution "falls through" to the next case's statements until a break or the end of
the switch is reached.
d) Only the default case is executed.
Answer: c)
10. The expression in a switch statement can be of which types (traditionally, before
modern Java enhancements)?
a) Only boolean
b) byte, short, char, int, String, enum
c) Only float or double
d) Any object type
Answer: b) (The document doesn't specify this detail, but it's general Java knowledge
relevant to switch usage, and the example uses int)
IGNORE_WHEN_COPYING_START
content_copy download
Use code with caution. Java
IGNORE_WHEN_COPYING_END
20. If a break statement is used outside of a loop or a switch statement, what is the result?
a) It is ignored.
b) It causes the current method to return.
c) It results in a compile-time error.
d) It terminates the entire program.
Answer: c)
21. Which statement is used to "exit from the loop or switch case"?
a) goto
b) continue
c) break
d) pass
Answer: c)
22. If you want to stop processing the current iteration of a loop and immediately start the
next one, you would use:
a) break;
b) return;
c) continue;
d) next;
Answer: c)
23. In the example for the break statement, System.out.println("Loop exited."); is printed:
a) After each iteration of the loop.
b) Only if the loop completes all its iterations without a break.
c) Immediately after the break statement is executed, inside the loop.
d) After the loop has terminated, either by completing all iterations or by a break.
Answer: d)