Chapter 1
Chapter 1
Answer
Question 2
Smallest individual unit in a program is called ............... .
Answer
Question 3
Which of the following is not a token ?
keywords
identifiers
statement
operators
Answer
statement
Question 4
Identify the illegal identifier from the following.
_CHK
αβγτ
20_to_50
A_to_Z
Answer
20_to_50
Reason — An identifier must not begin with a digit. It may
contain underscore.
Question 5
Which of the following does not represent a character literal
?
'a'
'1'
'\a'
"a"
Answer
"a"
Question 6
Which keyword turns a variable declaration into constant
declaration ?
const
constant
final
fixed
Answer
final
Question 7
ch += 2 is equivalent to
ch = ch + 2
ch + 2
ch =+ 2
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.
java.util
java.io
java.random
java.lang
Answer
java.lang
Question 9
Which clause is optional in a switch statement?
switch
case
default
none of the above
Answer
default
Question 10
Absence of which statement causes a fall-through in a switch
statement.
continue
break
stop
fall
Answer
break
2
1
5
as many
Answer
Question 12
Which of the following loops is mostly used for fixed number
of iterations ?
for
do-while
while
none of the above
Answer
for
Question 13
Which of the following is not an entry controlled loop ?
for
do-while
while
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
Which of the following is an exit controlled loop?
for
do-while
while
none of the above
Answer
do-while
Question 15
Which of the following statements terminates the complete
execution of a loop ?
break
continue
terminate
System.exit(0)
Answer
break
Assignment Questions
Question 1
What is meant by token? Name the tokens available in Java.
Answer
Question 2
What are keywords ? Can keywords be used as identifiers ?
Answer
Question 3
What is an identifier ? What is the identifier forming rule(s) of
Java?
Answer
Answer
13 Integer Literal
'a' Character Literal
4.38925 Floating point Literal
"a" String Literal
main( ) Method
Question 5
What kind of constants are the following ?
14, 011, 0X2A, 17, 014, 0XBC1
Answer
Answer
Question 7(a)
Write an equivalent Java expression for the following
expressions:
ut
+
1
2
ft
2
ut+
2
1
ft
2
Answer
u * t + (1.0 / 2.0) * f * t * t
Question 7(b)
Write an equivalent Java expression for the following
expressions:
a
2
+
b
2
a
2
+b
2
Answer
Math.sqrt(Math.pow(a,2) + Math.pow(b,2))
Question 7(c)
Write an equivalent Java expression for the following
expressions:
ab + b >= ba + a
Answer
Question 7(d)
Write an equivalent Java expression for the following
expressions:
(
3
x
+
5
y
5
x
+
3
y
−
8
x
y
2
y
x
)
3
/
2
(
5x+3y
3x+5y
−
2yx
8xy
)
3/2
Answer
Question 8
What is meant by implicit and explicit type conversion ?
Answer
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
int a = 10;
double b = 25.5;
float c = (float)(a + b);
Question 10
What will be the resultant type of the following expression if
bh represents a byte variable, i is an int variable, fl is a float
variable and db is a double variable ?
bh - i + db / fl - i * fl + db / i
Answer
Explanation
bh - i + db / fl - i * fl + db / i
⇒ byte - int + double / float - int * float + double / int
⇒ byte - int + double - float + double
⇒ int + double - float + double
⇒ double - float + double
⇒ double + double
⇒ double
Question 11
What will be the resultant type of the following expression if
fl is a float variable and db is a double variable?
(int) (fl + db)
Answer
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
Determine the data type of the expression
(
100
(
1
−
p
q
)
(
q
+
r
)
)
−
(
(
p
+
r
)
/
s
(
long
)
(
s
+
p
)
)
(
(q+r)
100(1−pq)
)−(
(long)(s+p)
(p+r)/s
Answer
Explanation
(
int
∗
(
int
−
int
∗
long
)
(long + float)
)
−
(
(
int
+
float
)
/
double
(
long
)
(
double
+
int
)
)
⇒
(
int
∗
(
int
−
long
)
float
)
−
(
float
/
double
long
)
⇒
(
int
∗
int
float
)
−
(
double
long
)
⇒
float
−
double
⇒
double
(
(long + float)
int∗(int−int∗long)
)−(
(long)(double+int)
(int+float)/double
)
⇒(
float
int∗(int−long)
)−(
long
float/double
)
⇒(
float
int∗int
)−(
long
double
)
⇒float−double
⇒double
Question 13
Determine the data type of the expression
(
2
x
+
3
y
5
w
+
6
z
+
8
p
5
q
)
4
(
5w+6z
2x+3y
+
5q
8p
)
4
Answer
+
long double
short
)
4
⇒(
double
long
+
long double
short
)
4
⇒(double+long double)
4
⇒long double
Question 14(a)
State the value and type of each expression.
Math.abs(-5) - Math.abs(-7)
Answer
Question 14(b)
State the value and type of each expression.
Math.abs(-1e-1) + Math.abs(-2e-2)
Answer
Explanation
Math.abs(-1e-1) + Math.abs(-2e-2)
⇒ 0.1 + 0.02
⇒ 0.12
Question 14(c)
State the value and type of each expression.
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)
State the value and type of each expression.
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)
State the value and type of each expression.
Math.round(3.499)
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, 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)
State the value and type of each expression.
Math.max(1.5e-2, 0.095)
Answer
The output value is 0.095. The type of expression is double.
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)
State the value and type of each expression.
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)
State the value and type of each expression.
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)
State the value and type of each expression.
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)
State the value and type of each expression.
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)
State the value and type of each expression.
Math.pow(16, 0.25)
Answer
The output value is 2.0. The type of expression is double.
Explanation
Math.pow(x, y) returns x raised to the power of y as a double
value. Math.pow(16, 0.25) is equivalent to
16
4
4
16
Question 14(l)
State the value and type of each expression.
Math.pow(4, -2)
Answer
Question 14(m)
State the value and type of each expression.
Math.round(1.49 + 0.1)
Answer
Question 14(n)
State the value and type of each expression.
Math.round(1.49) + 0.1
Answer
Explanation
Math.round() rounds off its argument to the nearest
mathematical integer.
Math.round(1.49) + 0.1
⇒ 1 + 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.
a
2
−
b
2
a
2
−b
2
Answer
Math.sqrt(Math.pow(a,2) - Math.pow(b,2))
Question 15(b)
Write the following as Java expressions.
π(x6 - y6)
Answer
Question 15(c)
Write the following as Java expressions.
4
3
π
r
3
3
4
πr
3
Answer
Question 15(d)
Write the following as Java expressions.
| 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
Answer
(int)(6 * Math.random( )) + 1
Explanation
The formula to get an integer number between 1 and n is:
Question 17
What is the significance of a break statement in a switch
statement ?
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
for
while
do-while
Question 19
What is meant by an entry-controlled loop? Which Java loops
are entry-controlled?
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
What is meant by an exit-controlled loop ? Which Java loops
are exit-controlled ?
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.
Answer
Answer
Question 23
What is nested loop ?
Answer
For example:
1
21
321
4321
54321
Answer
12345
1234
123
12
1
Answer
1
10
101
1010
10101
Answer
//
*****
****
***
**
*
**
***
****
*****
Answer
import java.util.Scanner;
char
arrays
int
classes
Answer
(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)
Write a Java expression for the following :
3
x
+
x
2
a
+
b
a+b
3x+x
2
Answer
Math.sqrt(3 * x + Math.pow(x,2)) / (a + b))
Question 30(b)
What is the value of y after evaluating the expression given
below ?
Answer
Explanation
y += ++y + y-- + --y
⇒ y = y + ++y + y-- + --y y=8
⇒ y = 8 + 9 + y-- + --y y=9
⇒ y = 8 + 9 + 9 + --y y=8
⇒y=8+9+9+7 y=7
⇒ y = 33
Question 30(c)
Give the output of the following :
Math.floor(-4.7)
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
What are the values stored in variables r1 and r2 ?
r1 has 5.83
r2 has 4.0
Explanation
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.
Math.floor(16.3) returns 16.0. Math.sqrt(16.0) gives square
root of 16.0 which is 4.0.
Question 32(a)
Name the operators listed below :
<
++
&&
?:
Answer
Answer
Question 32(c)
Write one difference between / and % operator.
Answer
/ operator computes the quotient whereas % operator
computes the remainder.
Question 33
Predict the output :
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
z = Math.sqrt(x * x + y * y)
⇒ Math.sqrt(3.0 * 3.0 + 4.0 * 4.0)
⇒ Math.sqrt(9.0 + 16.0)
⇒ Math.sqrt(25.0)
⇒ 5.0
Question 34
Predict the output :
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
The execution of while loop is shown in the table below:
Question 35
Predict the output :
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
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.
Output
20
Explanation
Initially i = 5.
Question 37
Find the error
x = 3;
y = 4;
z = math.power(x*x, y/2);
Answer
int x = 3;
int y = 4;
double z = Math.pow(x*x, y/2);
Question 39
Find the error:
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
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;
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
BlueJ output of KboatCheckCase.java
BlueJ output of KboatCheckCase.java
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
BlueJ output of KboatCheckLetterDigitSpChar.java
BlueJ output of KboatCheckLetterDigitSpChar.java
BlueJ output of KboatCheckLetterDigitSpChar.java
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
BlueJ output of KboatVowelCheck.java
BlueJ output of KboatVowelCheck.java
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;
import java.util.Scanner;
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
BlueJ output of KboatDigitCount.java
BlueJ output of KboatDigitCount.java
Question 47
Write a program that prints the squares of 10 even numbers
in the range 10 .. 100.
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
BlueJ output of KboatPalindromeNumber.java
BlueJ output of KboatPalindromeNumber.java
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;
long f = 1;
System.out.println("Factorial of " + n
+ " = " + f);
}
}
Output
BlueJ output of KboatFactorial.java
Question 53
Write a program to print Floyd's triangle as shown below:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
import java.util.Scanner;