Chapter 8 Conditional Constructors in Java EMCQs
Chapter 8 Conditional Constructors in Java EMCQs
3. Which among the following relational operator is used to check for the equality between
two
quantities ?
a. =
b. ==
c. equals
d. None of these
Ans. b. ==
6. What is the problem called in which an optional else clause in an if–then(–else) statement
results in nested conditionals being ambiguous?
a. Dangling if
b. Dangling else
c. Both a and b
d. None of these
Ans. b. Dangling else
8. State whether the following statements about switch statement are correct.
i. Switch statement often provides a better alternative than a large series of if-else-if
statements.
ii. The break statement is used inside the switch to terminate a statement sequence.
a. True, False
b. False, True
c. True, True
d. False, False
Ans. c. True, True
9. The conditional statement, can only test for equality, whereas can evaluate any type of
Boolean
expression.
a. if, switch
b. switch, if
c. while, if
d. if, while
Ans. b. switch, if
2.
Using conditional operator write equivalent expressions for the following:
i. F=0.5 if x=30 otherwise 0.
Ans. F=(x==30)?0.5:0;
ii. Print “Even” if x is an even number otherwise print “Odd”.
Ans. System.out.println((x%2==0)? “Even”: “Odd”;
iii. Rewrite the following using Ternary operator
if (income < = 10000 )
tax = 0;
else
tax = 12;
Ans. tax=(income<=10000)?0:12;
3.
Find the output of the following program segment, when:
i. val = 500
ii. val = 1600
4.
Write the conditional statement for the following decision making situation: Provided the
value of x is greater than 9, increase the value of y by 15, otherwise decrease the value of
y by 36.
Ans. y=(x>9)?y+15:y-36;
5.
Rewrite the following program segment using if-else statements instead of the ternary
operator.
char grade=(mark>=90)? “A” : (mark>=80)? “B” : “C”;
Ans. char grade;
If (mark>=90)
grade=’A’;
else if(mark>=80)
grade=’B’;
else
grade=’C’;
6.
Write equivalent Java statements for the following situations:
i. X is greater than equal to Y.
Ans. X>=Y
ii. X is not equal to Y.
Ans. X!=Y
iii. X is equal to Y.
Ans. X==Y
iv. X is less than Y and X is less than Z.
Ans. X<Y && X<Z
v. Either X is greater than Y or X is less than Z.
Ans. X>Y || X<Z
vi. If X has a value greater than 20 then increment the value of J by 1 otherwise
decrement
the value by 2.
Ans. J=(X>20)?J+1:J-2;
vii. It is not that X is greater than Y.
Ans. !(X>Y)
7.
The following is a segment of a program:
x=1; y=1;
if(n>0)
{
y=y-1;
x=x+1;
}
What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
Ans. i. x=2 and y=0
ii. X=1 and y=1
8.
What is the output of the following program:
class First
{
static void check()
{
float num=110;
System.out.print((num!=-num)? “OK”: “NOT”);
}
}
Ans. OK
9.
What is the output of the following program:
class Second
{
static void check()
{
int r, a=50, b=10;
r=(a>45) ?a : b;
System.out.print(r);
}
}
Ans. 50
10. Rewrite the following set of if-else statement in terms of switch case statement for the
following code fragment:
int code;
code=sc.nextInt();
if (code==1)
System.out.print(“Accountant”);
else
if (code==5 || code==6)
System.out.print(“Grade IV”);
else
if (code==3)
System.out.print(“Financial Advisor”);
Ans.
int code;
code=sc.nextInt();
switch(code)
{
case 1:
System.out.print(“Accountant”);
case 5:
case 6:
System.out.print(“Grade IV”);
case 3:
System.out.print(“Financial Advisor”);
}
11. Find the syntax error(s), if any, in the following program and rewrite it after correction:
Class First
{
public static void main(String args[]){
int R; W=90;
R=W-50;
Switch(W)
{
20:system.out.print(Lower Range”);
30:System.Out.print(“Middle Range”)
40:System.out.Print(“Higher Range);
}
}
Ans.
class First
{
public static void main(String args[]){
int R,W=90;
R=W-50;85
Computer Applications – IX (ICSE Course) Answers
switch(R)
{
case 20:
System.out.print(“Lower Range”);
break;
case 30:
System.out.print(“Middle Range”)
break;
case 40:
System.out.print(“Higher Range”);
}
}
3. Which of the following loops will execute the body of loop even when condition
controlling the loop is initially false?
a) do-while
b) while
c) for
d) none of the mentioned
Answer: a
4. Which of these jump statements can skip processing the remainder of the code in
its body for a particular iteration?
a) break
b) return
c) exit
d) continue
Answer: d
6. What will be the output of the following Java program?
1. class selection_statements
2. {
3. public static void main(String args[])
4. {
5. int var1 = 5;
6. int var2 = 6;
7. if ((var2 = 1) == var1) //here var 2 again gets initialized
8. System.out.print(var2);
9. else
10. System.out.print(++var2);
11. }
12. }
a) 1
b) 2
c) 3
d) 4
Answer: b
1. class comma_operator
2. {
3. public static void main(String args[])
4. {
5. int sum = 0;
6. for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
7. sum += i;
8. System.out.println(sum);
9. }
10. }
a) 5
b) 6
c) 14
d) compilation error
Answer: b
1. class jump_statments
2. {
3. public static void main(String args[])
4. {
5. int x = 2;
6. int y = 0;
7. for ( ; y < 10; ++y)
8. {
9. if (y % x == 0)
10. continue;
11. else if (y == 8)
12. break;
13. else
14. System.out.print(y + " ");
15. }
16. }
17. }
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
Answer: c
1. class Output
2. {
3. public static void main(String args[])
4. {
5. int a = 5;
6. int b = 10;
7. first:
8. {
9. second:
10. {
11. third:
12. {
13. if (a == b >> 1) // it has somehow got executed
14. break second;
15. }
16. System.out.println(a);
17. }
18. System.out.println(b);
19. }
20. }
21. }
a) 5 10
b) 10 5
c) 5
d) 10
Answer: d
B) To create objects
C) To declare variables
Answer: Option D
B) for loop
C) if-else
D) do-while loop
Answer: Option C
5.) Which control statement is used to execute different code blocks based
on the value of an expression?
A) switch-case
B) if-else
C) for loop
D) while loop
Answer: Option A
C) It is executed when none of the cases match the value of the expression.
D) None of these.
Answer: Option C
Answer: Option D
Explanation: The “return” statement is used to exit a method and return a
value to the caller of the method.
B) switch-case
C) for loop
D) do-while loop
Answer: Option C
Explanation: A “for” loop repeatedly executes a block of code as long as
the specified condition is true.
B) Runtime error.
Answer: Option C
1) What is the other name for a Question Mark - Colon
(?:) operator in Java?
A) Special Relational operator
B) Special Logical Operator
C) Ternary Operator
D) None
Answer [=]
C
2) Java Ternary operator is sometimes called ____.
A) Relational Operator
B) Conditional Operator
C) Logical Operator
D) None
Answer [=]
B
4) State TRUE or FALSE. True expression part comes
first after ? (question mark) symbol and before :
(colon) symbol.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
B
6) A java Ternary operator has priority less than ___.
A) Relational operators
B) Arithmetic operators
C) Logical and bitwise operators
D) All
Answer [=]
D
1) An IF-ELSE statement is also called ___.
A) Branching statement
B) Control statement
C) Block statements
D) All
Answer [=]
D
6) An ELSE statement must be preceded by ___ statement
in Java.
A) IF
B) ELSE IF
C) IF or ELSE IF
D) None
Answer [=]
C
11) The condition of an IF statement evaluates to
boolean only if the expression contains?
A) logical operators
B) relational operators
C) boolean operands
D) All
Answer [=]
D
14) An IF-ELSE statement is better than a SWITCH
statement in which scenario below?
A) Checking for More-than condition
B) Checking for Less-than condition
C) Checking for Ranges
D) All
Answer [=]
D
1) A SWITCH case statement in Java is a ___ control
statement.
A) Iteration
B) Loop
C) Selection
D) Jump
Answer [=]
C
6) A switch statement in Java accepts ___ as input
data.
A) enum
B) String
C) enum and String
D) long
Answer [=]
C
8) Which version of Java did start supporting String
as the input data type of a SWITCH?
A) JDK 5
B) JDK 6
C) JDK 7
D) JDK 8
Answer [=]
C
17) A SWITCH fall through occurs in Java only in the
absence of ___.
A) case keyword
B) break keyword
C) default keyword
D) None
Answer [=]
B
21) Choose TRUE or FALSE. A SWITCH can be used to
compare values for high or low.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
A
6. In the while and do-while loops, a ………………… statement causes control to be transferred
directly to the conditional expression that controls the loop.
A) break
B) pause
C) start
D) continue
Answer: d
13. The ………………………. loop is especially useful when you process a menu selection.
A) while
B) do-while
C) for
D) switch
Answer: B
20. State whether the following statements for if statement are True or False.
i) When if statements are nested, the last else gets associated with the nearest if without an else.
ii) One if can have more than one else clause.
A) True, False
B) False, True
C) True, True
D) False, False
Answer: C
18. The ……………………… statement at the end of each block signals the end of a particular case
and causes an exit from the switch statement, transferring the control to the statement following the
switch.
A) switch
B) break
C) continue
D) default
19. Answer: B
8. State whether the following statements about conditional statements are True or False.
i) The break statement is required in the default case of a switch selection structure.
ii) A variable declared inside the for loop control cannot be reference outside the loop.
A) True, False
B) False, True
C) True, True
D) False, False
Answer: B