TillSimpleLoops_2024
TillSimpleLoops_2024
5 hour
Question 1: Choose the correct answer and write the correct option (1M * 20)
A=b A==b
It is an assignment operator It is an equality operator
It is used to assign value from b to a It is used to compare if values in a and b are
exactly equal or not
3. Differentiate between entry controlled loop and exit controlled loop with example.
Entry Exit
It checks the condition at the time of entry. It checks the condition after executing its
Only if the condition is true, the program body. If the condition is true, loop will
control enters the body of the loop. perform the next iteration otherwise program
control will move out of the loop.
Loop does not execute at all if the condition is The loop executes at least once even if the
false. condition is false.
Ex: for, while Ex: do-while
break statement at the end of case is optional. Omitting break leads to program execution
continuing into the next case and onwards till a break statement is encountered or end of switch is
reached. This is termed as Fall Through in switch case statement.
break statement is used to unconditionally jump out of the loop whereas continue statement is
used to unconditionally jump to the next iteration of the loop, skipping the remaining statements
of the current iteration.
break statement is used in switch-case and loops whereas continue statement is only used in
loops.
6. Write the output of the following program code if char ch ; int x=97;
do {
ch=(char) x;
System.out.print(ch + “ ” );
if(x%10 == 0)
break;
++x;
}while(x<=100);
abcd
7. Convert the following while loops into for loops. Also write the output of the program
int i, x = 0;
while (i<=20){
System.out.println(i+ “ ”);
x++;}
int i, x = 0;
for (; i<=20 ; x++){
System.out.println(i+ “ ”);
x++;
}
compile time error
Tokens are individual components of a java statement that carry out certain functions and
participate in the program. Ex: variable, keyword etc
Java keywords or reserved words are the words which carry a special meaning for the system
compiler. These words are basically used for writing a Java statement in the program. Such words
cannot be used for naming a variable in the program.
In static initialization, the initial value of the variable is provided as a literal at the time of
declaration. For example:
In dynamic initialization, the initial value of the variable is the result of an expression or the
return value of a method call. Dynamic initialization happens at runtime. For example:
int a = 4;
int b = Math.sqrt(a);
In pure expressions, all the operands are of same type. The result is the same type as the values
Ex: int x =1, y=2, z= 3;
int s = x+y+z
In mixed expressions, the operands are of mixed or different data types. The result is of the higher
data types
Ex: double s = 2+’a’+3.0;
11. Convert the following to if-else:
int x = a % 2 == 0 ? 1 : a%3 ==0 ? 0 : -1
int x;
if(a%2==0) x=1;
else if(a%3==0) x=0;
else x=-1;
14. Define comments and mention the types of comments along with the syntax
Java comments are notes in the source code of a Java program that are ignored by the compiler.
They are used to provide explanations, descriptions, or any relevant information about the code. It
makes it easier to understand for humans.
Types of comments:
Single line : //
Multi Line : /*. */
Documentaiton : /** */
15. Name the following:
a. One thing in many different forms : polymorphism
b. Method that returns the largest double value that is less than or equal to the argument
and is equal to a mathematical integer : Math.floor()
c. The keyword in a switch case, when the switch value does not match with any case
default
d. Two or more statements that can be grouped together by enclosing them between
opening and closing curly braces : compound statement
1) Using the switch statement, write a menu driven program to perform following operations:
switch (choice) {
case 1:
double Z, Y = 5.5;
for (int x = -10; x <= 10; x+= 2) {
Z = (Math.pow(x, 3) + 0.5 * x) / Y;
System.out.println("Value of Z when x is "
+ x + " = " + Z);
}
break;
case 2:
System.out.print("Enter number of terms: ");
int N = in.nextInt();
System.out.print("Enter a: ");
double a = in.nextDouble();
default:
System.out.println("Incorrect Choice");
break;
}
}
}
2) Write a program to take a n-digit number as input. Find if the number is Armstrong or not.
Also check if number is palindrome or not and print output as follows:
Ex: 153 Output: Number is Armstrong but not palindrome
Ex: 121 Output: Number is not Armstrong but palindrome
Ex: 132 Output: Number is neither Armstrong nor palindrome
import java.util.Scanner;
public class Armstrong {
originalNumber = number;
while (originalNumber != 0)
{
c++;
originalNumber /= 10;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, c);
originalNumber /= 10;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
rev = rev * 10 + remainder;
originalNumber /= 10;
}
}
}
}