Page 271 - IT-802_class_12
P. 271
Example
Operator Description Explanation Result
(int a = 20, b = 30)
+ Addition Returns the sum of values of operands a+b 50
- Subtraction Returns the difference of values of operands a-b -10
* Multiplication Returns the product of values of operands a*b 600
/ Division Returns the quotient of division of values of b/a 1
operands
% Modulus Returns the remainder of division of values a%b 10
of operands
++ Increment Increments the operand by 1 a++ Or ++a 21
-- Decrement Decrements the operand by 1 a-- Or --a 29
3. What is if else statement? Write it`s syntax.
Ans. The if statement in Java lets us execute a block of code depending upon whether an expression evaluates to true or false.
The syntax of the Java if statement is as below:
if (expression)
{
statements
}
4. What do you mean by exception handling?
Ans. When exception occurs, the program stops immediately with a system generated message. To provide a user-friendly
message, we need to handle the raised exceptions. Using it, we can ensure that the flow of the program doesn’t break when
an exception occurs. So, exception handling is a mechanism to handle errors occurred during execution of the program code
so that the normal flow of the code can be maintained.
5. What will be the value of rem after the execution of the following code snippet? why? [CBSE Sample Paper 2023]
code =2;
switch(code)
{case 1: rem= 8;
case 2: rem= 9;
case 3: rem= 10; break;
case 4: rem= 11; break;}
Ans. 10
Fall through statement since there is no break in case 2
6. What is the difference between a while and a do-while loop? [CBSE Sample Paper 2023]
Ans. A while loop is an entry controlled loop it tests for a condition prior to running a block of code. A do-while loop is an exit
control loop.
Or
A while loop runs zero or more times Body of loop may never be executed A do-while loop runs once or more times but at
least once.
7. Consider the following code: [CBSE Sample Paper 2023]
int x = 1;
while(x<=5);
x = x +1;
(a) Name the coding error shown in the above code.
(b) What is the reason of the error?
(c) Write correct java code.
Fundamentals of Java Programming 269

