Revision of Class IX
Revision of Class IX
Question 1
Answer
Question 2
Answer
Question 3
1. keywords
2. identifiers
3. statement
4. operators
Answer
statement
Question 4
1. _CHK
2. αβγτ
3. 20_to_50
4. A_to_Z
Answer
20_to_50
Reason — An identifier must not begin with a digit. It may contain underscore.
Question 5
1. 'a'
2. '1'
3. '\a'
4. "a"
Answer
"a"
Reason — Since a is enclosed in double quotes (" "), it becomes a String literal. Character
literals are always enclosed within single quotes (' ').
Question 6
1. const
2. constant
3. final
4. fixed
Answer
final
Question 7
ch += 2 is equivalent to
1. ch = ch + 2
2. ch + 2
3. ch =+ 2
4. none of the above
Answer
ch = ch + 2
Reason — += is a shorthand operator. The operator pair += tells the compiler to assign
to ch the value of ch + 2.
Question 8
The Math class is part of which Java library package.
1. java.util
2. java.io
3. java.random
4. java.lang
Answer
java.lang
Question 9
1. switch
2. case
3. default
4. none of the above
Answer
default
Reason — The default clause is optional and, if it is missing, no action takes place if all
matches fail.
Question 10
1. continue
2. break
3. stop
4. fall
Answer
break
Reason — In the absence of break statement, the control flows to the next case below the
matching case leading to fall-through.
Question 11
By default, the if-part and else-part of an if statement can contain these many statements in it.
1. 2
2. 1
3. 5
4. as many
Answer
Reason — By default, the if-part and else-part can contain only one statement. To include
more than one statements, the set of statements need to be enclosed in curly braces and
included as one compound statement.
Question 12
Which of the following loops is mostly used for fixed number of iterations ?
1. for
2. do-while
3. while
4. none of the above
Answer
for
Reason — for loop is usually used when the number of iterations are fixed.
Question 13
1. for
2. do-while
3. while
4. none of the above
Answer
do-while
Reason — do-while is an exit controlled loop as it executes atleast once even when the
condition is false.
Question 14
1. for
2. do-while
3. while
4. none of the above
Answer
do-while
Reason — do-while is an exit controlled loop as it executes atleast once even when the
condition is false. The condition is checked at the time of exit.
Question 15
1. break
2. continue
3. terminate
4. System.exit(0)
Answer
break
Assignment Questions
Question 1
Answer
Keywords, identifiers, literals, operators and punctuators are tokens available in Java.
Question 2
Answer
Keywords are the words with special meaning associated with them. These are reserved for
special purpose and must not be used as normal identifier names. Some keywords of Java are
default, return, if, private etc.
Answer
Identifiers are the names given by the programmer to various program units of Java.
Identifiers are the names of variables, methods, classes, packages and interfaces etc.
1. Identifiers can have alphabets, digits, _ (underscore) and $$ characters and can be of
any length.
2. They must not be a keyword or Boolean literal or null literal.
3. They must not begin with a digit.
4. Java is case sensitive i.e., upper-case letters and lower-case letters are treated
differently.
Question 4
Answer
13 Integer Literal
main( ) Method
Question 5
Answer
Question 6
What is a character constant in Java ? How are non graphic characters represented in Java ?
Answer
Single character enclosed in single quotation marks (' ') makes a character literal. For
example, 'a', '5', $$, '1' are character literals.
Non-graphic characters are represented in Java using escape sequence. An escape sequence is
represented by a backslash (\) followed by one or more characters. For example, '\n', '\t' are
escape sequences for new line and tab respectively.
Question 7(a)
ut+12ft2ut+21ft2
Answer
u * t + (1.0 / 2.0) * f * t * t
Question 7(b)
a2+b2a2+b2
Answer
Math.sqrt(Math.pow(a,2) + Math.pow(b,2))
Question 7(c)
Answer
Math.pow(a, b) + b >= Math.pow(b, a) + a
Question 7(d)
(3x+5y5x+3y−8xy2yx)3/2(5x+3y3x+5y−2yx8xy)3/2
Answer
Math.pow(((3 * x + 5 * y) / (5 * x + 3 * y)) - ((8 * x * y) / (2 * y * x)),
3.0 / 2)
Question 8
Answer
In an implicit conversion, the result of a mixed mode expression is obtained in the higher
most data type of the variables without any intervention by the user. For example:
int a = 10;
float b = 25.5f, c;
c = a + b;
In case of explicit type conversion, the data gets converted to a type as specified by the
programmer. For example:
int a = 10;
double b = 25.5;
float c = (float)(a + b);
Question 9
What do you mean by type casting? What is type cast operator in Java?
Answer
Type Casting in Java is done using the type cast operator. It is a unary operator. It's syntax is:
int a = 10;
double b = 25.5;
float c = (float)(a + b);
Question 10
bh - i + db / fl - i * fl + db / i
Answer
Explanation
Question 11
What will be the resultant type of the following expression if fl is a float variable and db is
a double variable?
Explanation
Here, the programmer is performing an explicit type conversion to int using the type cast
operator. Hence, the resultant data type will be int.
Question 12
(100(1−pq)(q+r))−((p+r)/s(long)(s+p))((q+r)100(1−pq))−((long)(s+p)
(p+r)/s)
If p is an int, r is a float, q is a long and s is double.
Answer
(int∗(int−int∗long)(long + float))−((int+float)/double(long)
(double+int))⇒(int∗(int−long)float)−(float/doublelong)⇒(int∗intfloat)−
(doublelong)⇒float−double⇒double((long + float)int∗(int−int∗long))−
((long)(double+int)(int+float)/double)⇒(floatint∗(int−long))−(longfloat/
double)⇒(floatint∗int)−(longdouble)⇒float−double⇒double
Question 13
(2x+3y5w+6z+8p5q)4(5w+6z2x+3y+5q8p)4
if x is int, y is long, w is float, z is double, p is short and q is long double.
Answer
Explanation
Question 14(a)
Math.abs(-5) - Math.abs(-7)
Answer
Explanation
⇒5-7
Math.abs(-5) - Math.abs(-7)
⇒ -2
Question 14(b)
Math.abs(-1e-1) + Math.abs(-2e-2)
Answer
Explanation
⇒ 0.1 + 0.02
Math.abs(-1e-1) + Math.abs(-2e-2)
⇒ 0.12
Question 14(c)
Math.sqrt(0.0064)
Answer
Explanation
Math.sqrt() method returns the square root of its argument. As square root of 0.0064 is 0.08
hence that is the output. The return type of Math.sqrt() is double so the type of expression is
double.
Question 14(d)
Math.sqrt(Math.pow(2.7, 2))
Answer
Explanation
Math.pow(2.7, 2) will calculate square of 2.7 as we are raising it to the power 2. After this,
calculating square root with Math.sqrt() will return the same number 2.7. The return type of
Math.sqrt() is double so the type of expression is double.
Question 14(e)
Math.round(3.499)
Answer
Math.round() rounds off its argument to the nearest mathematical integer. If argument is
float, return type is int, if argument is double, return type is long. In this case, 3.499 will be
treated as double because suffix 'f' is not there. Hence, it will return a value of long data type.
Question 14(f)
Math.max(1.5e-2, 0.095)
Answer
Explanation
Math.max(a, b) returns the maximum of a and b. As 0.095 is greater than 1.5e-2, hence, 0.095
is the output. Since the argument is double so the data type of return value is also double.
Question 14(g)
Math.ceil(4.002)
Answer
Explanation
Math.ceil(x) returns the smallest double value that is greater than or equal to the argument
and is equal to a mathematical integer. Hence, output is 5.0 and data type of result is double.
Question 14(h)
Math.min(-5, 1.0)
Answer
Explanation
Math.min(a, b) returns the minimum of a and b. As one of the arguments is of double type
hence, data type of return value is also double.
Question 14(i)
Math.floor(7.99)
Answer
Explanation
Math.floor( ) returns the largest double value that is less than or equal to the argument and is
equal to a mathematical integer. Hence, output is 7.0 and data type of result is double.
Question 14(j)
Math.ceil(-2.73)
Answer
Explanation
Math.ceil( ) returns the smallest double value that is greater than or equal to the argument and
is equal to a mathematical integer. -2.0 is the smallest mathematical integer greater than -
2.73. Hence, output is -2.0 and data type of result is double.
Question 14(k)
Math.pow(16, 0.25)
Answer
Explanation
Question 14(l)
Math.pow(4, -2)
Answer
Explanation
Question 14(m)
Math.round(1.49 + 0.1)
Answer
Explanation
Math.round() rounds off its argument to the nearest mathematical integer. If argument is
float, return type is int, if argument is double, return type is long. In this case, 1.49 + 0.1 =
1.59 so it will be rounded of to 2, the nearest mathematical integer. 1.59 will be treated as
double because suffix 'f' is not there. Hence, it will return a value of long data type.
Question 14(n)
Math.round(1.49) + 0.1
Answer
Explanation
⇒ 1 + 0.1
Math.round(1.49) + 0.1
⇒ 1.1
If argument is float, return type is int, if argument is double, return type is long. In this case,
1.49 will be treated as double because suffix 'f' is not there. Hence, it will return a value of
long data type.
Question 15(a)
Write the following as Java expressions.
a2−b2a2−b2
Answer
Math.sqrt(Math.pow(a,2) - Math.pow(b,2))
Question 15(b)
π(x6 - y6)
Answer
Math.PI * (Math.pow(x,6) - Math.pow(y,6))
Question 15(c)
43πr334πr3
Answer
4.0 / 3.0 * Math.PI * Math.pow(r,3)
Question 15(d)
| z4 - 1 |
Answer
Math.abs(Math.pow(z,4) - 1)
Question 16
A student incorrectly attempted to produce a random value in the range 1.6 using the
expression.
6*(int)Math.random( ) + 1
Correct the error in expression above to get the desired result.
Answer
Question 17
Answer
The break statement when used inside a switch, ends that case and proceeds to the first
statement that follows switch statement. In case, the break statement is missing, the control
flows to the next case below the matching case and continues to execute all the cases, till the
end of switch statement. This is called fall through.
Question 18
What are iteration statements ? Name the iteration statements provided by Java.
Answer
1. for
2. while
3. do-while
Question 19
Answer
The loop which tests the condition before entering the loop is called entry-controlled loop. It
does not execute if the condition is false.
Question 20
Answer
If a loop tests the condition at the time of exit from the loop, it is called exit-controlled loop.
This loop executes at least once even if the condition is false.
Question 21
Answer
while loop checks the test condition at the beginning do-while loop checks the test condition at the end
of the loop. the loop.
while loop is helpful in situations where number of do-while loop is suitable when we need to display
iterations is not known. menu to the user.
Syntax: Syntax:
while(test condition) do
{ {
... ...
} }while(condition);
Question 22
How many times is the loop body executed in a do loop, even if the test-condition is false ?
Answer
do-while loop is an exit controlled loop. Thus, its body is executed atleast once even if the
test-condition is false.
Question 23
Answer
A loop may contain another loop in its body. This form of a loop is called nested loop. In a
nested loop, the inner loop must terminate before the outer loop.
For example:
Question 24
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Answer
Output
Question 25
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Answer
Output
Question 26
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
Answer
//
Output
Question 27
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Answer
import java.util.Scanner;
Output
Question 28
1. char
2. arrays
3. int
4. classes
Answer
Question 29
System.out.print("BEST");
System.out.println("OF LUCK");
Choose the correct option for the output of the above statements
(ii) BEST
OF LUCK
Answer
Explanation
System.out.print does not print a newline at the end of its output so the println statement
begins printing on the same line. So the output is BEST OF LUCK printed on a single line.
Question 30(a)
3x+x2a+ba+b3x+x2
Answer
Math.sqrt(3 * x + Math.pow(x,2)) / (a + b))
Question 30(b)
Explanation
⇒y=8+9+9+7
y=8
⇒ y = 33
y=7
Question 30(c)
1. Math.floor(-4.7)
2. Math.ceil(3.4) + Math.pow(2,3)
Answer
(1) Math.floor(-4.7)
Output
-5.0
Explanation
Math.floor method returns the largest double value that is less than or equal to the argument
and is equal to a mathematical integer. As -5.0 is the largest mathematical integer less than -
4.7 so it is the output. Note that -4.7 is a negative number so the largest integer less than -4.7
is -5.0 and not -4.0.
Output
12.0
Explanation
Math.ceil(x) function returns the smallest whole number greater than or equal to x.
Math.ceil(3.4) gives 4.0 and Math.pow(2,3) gives 8.0. So the output is 4.0 + 8.0 = 12.0.
Question 31
Answer
1. r1 has 5.83
2. r2 has 4.0
Explanation
1. Math.min(-2.83, -5.83) returns -5.83 as -5.83 is less than -2.83. (Note that these are
negative numbers). Math.abs(-5.83) returns 5.83.
2. Math.floor(16.3) returns 16.0. Math.sqrt(16.0) gives square root of 16.0 which is 4.0.
Question 32(a)
1. <
2. ++
3. &&
4. ?:
Answer
Question 32(b)
State the number of bytes occupied by char and int data types.
Answer
Question 32(c)
Answer
Question 33
class Test {
public static void main (String args[]) {
double x, y, z ;
x = 3;
y = 4;
z = Math.sqrt(x * x + y * y);
System.out.println("z = " + z);
}
}
Answer
Output
z = 5.0
Explanation
⇒ Math.sqrt(9.0 + 16.0)
⇒ Math.sqrt(25.0)
⇒ 5.0
Question 34
class Power {
public static void main(String args[]) {
int e = 5, result, i;
result = 1 ;
i = e ;
while(e > 0) {
result *= 2 ;
e--;
}
int n = result /2, p = i - 1;
System.out.println("2 to the power of " + i + " is "
+ result);
System.out.println("2 to the power of " + p + " is " +
n);
}
}
Answer
Output
2 to the power of 5 is 32
2 to the power of 4 is 16
Explanation
⇒ n = 32 / 2, p = 5 - 1
n = result / 2, p = i - 1;
⇒ n = 16, p = 4
Question 35
class FindFac {
public static void main (String args[]) {
for(int i = 2; i <= 100; i++) {
System.out.print("Factors of" + i + ": ");
for(int j = 2; j < i; j++)
if((i % j) == 0)
System.out.print(j + " ");
System.out.println();
}
}
}
Answer
The output of the program is a list of the factors of each integer between 2 and 100, printed to
the console.
Output
Factors of2:
Factors of3:
Factors of4: 2
Factors of5:
Factors of6: 2 3
Factors of7:
Factors of8: 2 4
Factors of9: 3
Factors of10: 2 5
...
...
Factors of95: 5 19
Factors of96: 2 3 4 6 8 12 16 24 32 48
Factors of97:
Factors of98: 2 7 14 49
Factors of99: 3 9 11 33
Factors of100: 2 4 5 10 20 25 50
Explanation
This Java program finds the factors of integers between 2 and 100.
The program uses two nested for loops to generate the integers to be factored and to find
their factors. The outer for loop generates the numbers from 2 to 100 whose factors are to be
found.
The inner for loop starts with j = 2 and continues until j < i, incrementing j by 1 each
time through the loop. This loop checks whether j is a factor of i by checking whether the
remainder of i divided by j is equal to zero. If j is a factor of i, it is printed to the console on
the same line as the message "Factors of [i]: ".
After the inner loop has completed, a newline character is printed to the console
using System.out.println(). This causes the next message ("Factors of [i+1]: ") to be
printed on a new line.
Let us consider the execution of nested 'for' loop for a single value of i (say, i = 6). The given
table follows the execution:
i <= (i % j)
i j j<i Output Remarks
100 == 0
Factors of 6:
6 True 2 True True 6%2=0
2
Factors of 6:
3 True True 6%3=0
23
Give the output of the following program segment and also mention how many times the loop
is executed.
int i;
for (i = 5 ; i > 10; i++)
System.out.println(i);
System.out.println(i * 4);
Answer
Output
20
Explanation
Initially i = 5.
The test condition i > 10 is false and so the control comes to the next statement following
'for' loop — System.out.println(i * 4); and prints 20 (5 * 4) on the screen.
Question 37
1. The variable count is not declared. It needs to be declared before it can be used.
2. The syntax of 'for' loop requires semicolons ( ; ) as separators, and not comma ( , ).
The correct code is as follows:
Question 38
x = 3;
y = 4;
z = math.power(x*x, y/2);
Answer
The errors are as follows:
1. The variables — x, y, z, are not declared. They need to be declared before they can be
used.
2. The letter 'm' in math should be in uppercase as Java is a case-sensitive language.
Also, the correct name of the function is pow. Hence, it must be written
as Math.pow().
int x = 3;
int y = 4;
double z = Math.pow(x*x, y/2);
Question 39
class Test {
public static void main (String args[]) {
int x = 10;
if(x == 10) {
int y = 20;
System.out.println("x and y: "+ x +" "+ y);
x = y * 2;
}
y = 100;
System.out.println("x is"+ x);
}
}
Answer
The variable y is declared inside 'if' block. Its scope is limited to if block and it cannot be
used outside the block.
class Test {
public static void main (String args[]) {
int x = 10;
int y;
if(x == 10) {
y = 20;
System.out.println("x and y: "+ x +" "+ y);
x = y * 2;
}
y = 100;
System.out.println("x is"+ x);
}
}
Question 40
Write a program that inputs a number and tests if the given number is a multiple of both 3
and 5.
import java.util.Scanner;
Output
Question 41
Write a program that inputs a character and prints if the typed character is in uppercase or
lowercase.
import java.util.Scanner;
if(Character.isUpperCase(ch))
System.out.println("Upper Case letter");
else if(Character.isLowerCase(ch))
System.out.println("Lower Case Letter");
else
System.out.println("Character not an alphabet");
}
}
Output
Question 42
Write a program that inputs a character and prints if the user has typed a digit or an alphabet
or a special character.
import java.util.Scanner;
if(Character.isLetter(ch))
System.out.println("Letter");
else if(Character.isDigit(ch))
System.out.println("Digit");
else if(!Character.isWhitespace(ch))
System.out.println("Special character");
}
}
Output
Question 43
Write a program that inputs an alphabet and checks if the given alphabet is a vowel or not.
import java.util.Scanner;
if(ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U')
System.out.println("Vowel");
else
System.out.println("Not a vowel");
}
}
Output
Question 44
Write a program that takes a number and check if the given number is a 3 digit number or
not. (Use if to determine)
import java.util.Scanner;
Output
Question 45
Write a program to input three numbers and print the largest of the three numbers.
import java.util.Scanner;
Output
Question 46
Write a program that takes a number and check if the given number is a 3 digit number or
not. (Use a loop to determine)
import java.util.Scanner;
while (n != 0) {
count++;
n = n / 10;
}
if (count == 3)
System.out.println("Three digit number");
else
System.out.println("Not a three digit number");
}
}
Output
Question 47
Write a program that prints the squares of 10 even numbers in the range 10 .. 100.
Output
Question 48
Write a program that inputs a number and checks if the given number is a palindrome. A
number that is equal to its reversed number is a palindrome number.
import java.util.Scanner;
while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == num)
System.out.println("A Palindrome number");
else
System.out.println("Not a Palindrome number");
}
}
Output
Question 49
Write a program to input a number in the range 10 to 100 and check if it is a prime number.
import java.util.Scanner;
int c = 0;
Output
Question 50
Output
Question 51
Output
Question 52
import java.util.Scanner;
long f = 1;
System.out.println("Factorial of " + n
+ " = " + f);
}
}
Output
Question 53
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
import java.util.Scanner;