0% found this document useful (0 votes)
341 views15 pages

Java Selection and Control Statements Quiz

The document provides explanations for 10 multiple choice questions related to selection statements, loops, and flow control in Java. It defines selection statements as if and switch, and defines jump statements as break, continue, and return. It explains the differences between while, do-while, and for loops. The document also summarizes the purposes of various statements like break, continue, switch, and if/else.

Uploaded by

suraj raut
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
341 views15 pages

Java Selection and Control Statements Quiz

The document provides explanations for 10 multiple choice questions related to selection statements, loops, and flow control in Java. It defines selection statements as if and switch, and defines jump statements as break, continue, and return. It explains the differences between while, do-while, and for loops. The document also summarizes the purposes of various statements like break, continue, switch, and if/else.

Uploaded by

suraj raut
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
  • Selection Statements Questions
  • Basic Java Operations Questions
  • Loop Statements Questions
  • Switch and Decision Making Questions
  • String Manipulation and Comparisons Questions

1. Which of these selection statements test only for equality?

a) if

b) switch

c) if & switch

d) none of the mentioned

View Answer

Answer: b

Explanation: Switch statements checks for equality between the controlling variable and its constant
cases.

2. Which of these are selection statements in Java?

a) if()

b) for()

c) continue

d) break

View Answer

Answer:a

Explanation: Continue and break are jump statements, and for is an looping statement.

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

View Answer
Answer: a

Explanation: None.

4. Which of these jump statements can skip processing remainder of code in its body for a particular
iteration?

a) break

b) return

c) exit

d) continue

View Answer

Answer: d

Explanation: None.

5. Which of these statement is incorrect?

a) switch statement is more efficient than a set of nested ifs

b) two case constants in the same switch can have identical values

c) switch statement can only test for equality, whereas if statement can evaluate any type of boolean
expression

d) it is possible to create a nested switch statements

View Answer

Answer: b

Explanation: No two case constants in the same switch can have identical values.

6. What is the output of this program?

class selection_statements
{

public static void main(String args[])

int var1 = 5;

int var2 = 6;

if ((var2 = 1) == var1)

[Link](var2);

else

[Link](++var2);

a) 1

b) 2

c) 3

d) 4

View Answer

Answer:b

Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets
executed.

output:

$ javac selection_statements.java

$ java selection_statements

7. What is the output of this program?


class comma_operator

public static void main(String args[])

int sum = 0;

for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)

sum += i;

[Link](sum);

a) 5

b) 6

c) 14

d) compilation error

View Answer

Answer: b

Explanation: Using comma operator , we can include more than one statement in the initialization and
iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value – 0,1,2,3,4 &
j gets the values -0,1,2,3,4,5.

output:

$ javac comma_operator.java

$ java comma_operator

68. What is the output of this program?

class jump_statments
{

public static void main(String args[])

int x = 2;

int y = 0;

for ( ; y < 10; ++y)

if (y % x == 0)

continue;

else if (y == 8)

break;

else

[Link](y + " ");

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

View Answer

Answer:c

Explanation: Whenever y is divisible by x remainder body of loop is skipped by continue statement,


therefore if condition y == 8 is never true as when y is 8, remainder body of loop is skipped by continue
statements of first if. Control comes to print statement only in cases when y is odd.
output:

$ javac jump_statments.java

$ java jump_statments

13579

9. What is the output of this program?

class Output

public static void main(String args[])

final int a=10,b=20;

while(a<b)

[Link]("Hello");

[Link]("World");

a) Hello

b) run time error

c) Hello world

d) compile time error

View Answer
Answer: d

Explanation: Every final variable is compile time constant.

10. What is the output of this program?

class Output

public static void main(String args[])

int a = 5;

int b = 10;

first:

second:

third:

if (a == b >> 1)

break second;

[Link](a);

[Link](b);

}
}

a) 5 10

b) 10 5

c) 5

d) 10

Answer: d

Explanation: b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of if is executed and block
second is exited. Control goes to end of the block second executing the last print statement, printing 10.

output:

$ javac [Link]

$ java Output

10

1. What would be the output of the following codesnippet if variable a=10?

if(a<=0)

if(a==0)

[Link]("1 ");

else

[Link]("2 ");

[Link]("3 ");
a) 1 2

b) 2 3

c) 1 3

d) 3

View Answer

Answer: d

Explanation: Since the first if condition is not met, control would not go inside if statement and hence
only statement after the entire if block will be executed.

2. The while loop repeats a set of code while the condition is not met?

a) True

b) False

View Answer

Answer: b

Explanation: While loop repeats a set of code only until condition is met.

3. What is true about break?

a) Break stops the execution of entire program

b) Break halts the execution and forces the control out of the loop

c) Break forces the control out of the loop and starts the execution of next iteration.

d) Break halts the execution of the loop for certain time frame

View Answer

Answer: b

Explanation: Break halts the execution and forces the control out of the loop.

4. What is true about do statement?


a) do statement executes the code of a loop at least once

b) do statement does not get execute if condition is not matched in the first iteration

c) do statement checks the condition at the beginning of the loop

d) do statement executes the code more than once always

View Answer

Answer: a

Explanation: Do statement checks the condition at the end of the loop. Hence, code gets executed at
least once.

5. Which of the following is used with switch statement?

a) Continue

b) Exit

c) break

d) do

View Answer

Answer: c

Explanation: Break is used with switch statement to shift control out of switch.

6. What is the valid data type for variable “a” to print “Hello World”?

switch(a)

[Link]("Hello World");

a) int and float

b) byte and short


c) char and long

d) byte and char

View Answer

Answer: d

Explanation: The switch condition would only meet if variable “a” is of type byte or char.

7. Which of the following is not a decision making statement?

a) if

b) if-else

c) switch

d) do-while

View Answer

Answer: d

Explanation: do-while is an iteration statement. Others are decision making statements.

8. Which of the following is not a valid jump statement?

a) break

b) goto

c) continue

d) return

View Answer

Answer: b

Explanation: break, continue and return transfer control to another part of the program and returns
back to caller after execution. However, goto is marked as not used in Java.

9. From where break statement causes an exit?


a) Only from innermost loop

b) Terminates a program

c) Only from innermost switch

d) From innermost loops or switches

View Answer

Answer: d

Explanation: The break statement causes an exit from innermost loop or switch.

10. Which of the following is not a valid flow control statement?

a) exit()

b) break

c) continue

d) return

View Answer

Answer: a

Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running JVM.

Which of these class is superclass of String and StringBuffer class?

A. [Link] B. [Link] C. ArrayList D. None of the mentioned

Answer & Explanation

Answer: Option B

Explanation:

[Link]

2. Which of these operators can be used to concatenate two or more String objects?

A. + B. += C. & D. ||
Answer & Explanation

Answer: Option A

Explanation:

operator + is used to concatenate strings, Example String s = “i ” + “like ” + “java”; String s contains “I like
java”.

3. Which of these method of class String is used to obtain length of String object?

A. get() B. Sizeof() C. lengthof() D. length()

Answer & Explanation

Answer: Option D

Explanation:

Method length() of string class is used to get the length of the object which invoked method length().

4. Which of these method of class String is used to extract a single character from a String object?

A. CHARAT() B. charat() C. charAt() D. ChatAt()

Answer & Explanation

Answer: Option C

Explanation:

charAt()

5. Which of these constructors is used to create an empty String object?

A. String() B. String(void) C. String(0) D. None of the mentioned

Answer & Explanation

Answer: Option A

Explanation:

String()

7. Which of these method of class String is used to compare two String objects for their equality?

A. equals() B. Equals() C. isequal() D. Isequal()

Answer & Explanation


Answer: Option A

Explanation:

equals()

9. Which of these method of class String is used to check weather a given object starts with a particular
string literal?

A. startsWith() B. endsWith() C. Starts() D. ends()

Answer & Explanation

Answer: Option A

Explanation:

Method startsWith() of string class is used to check whether the String in question starts with a specified
string. It is specialized form of method regionMatches()

10. What is the value returned by unction compareTo() if the invoking string is less than the string
compared?

A. zero B. value less than zero C. value greater than zero D. None of the mentioned

Answer & Explanation

Answer: Option B

Explanation:

compareTo() function returns zero when both the strings are equal, it returns a value less than zero if
the invoking string is less than the other string being compared and value greater than zero when
invoking string is greater than the string compared to.

13.

What will s2 contain after following lines of code?

String s1 = “one”;

String s2 = [Link](“two”)
A. one B. two C. onetwo D. twoone

Answer & Explanation

Answer: Option C

Explanation:

Two strings can be concatenated by using concat() method.

14. Which of these method of class String is used to remove leading and trailing whitespaces?

A. startsWith() B. trim() C. Trim() D. doTrim()

Answer & Explanation

Answer: Option B

Explanation:

trim()

15. What is the value returned by function compareTo() if the invoking string is less than the string
compared?

A. zero B. value less than zero C. value greater than zero D. None of the mentioned

Answer & Explanation

Answer: Option B

Explanation:

compareTo() function returns zero when both the strings are equal, it returns a value less than zero if
the invoking string is less than the other string being compared and value greater than zero when
invoking string is greater than the string compared to.

Common questions

Powered by AI

The break statement, when used within a switch case, ends the execution of that case block and exits the switch statement. In loops, break causes the immediate termination of the loop and exits from it, transferring control to the statement following the loop .

The trim() method of the String class in Java removes leading and trailing whitespaces from a string, returning the modified string without affecting the original string .

The loop will halt when i becomes 5 because the loop continues as long as both conditions i < 5 and j < 5 are true. As the loop iterates, i increments by one with each iteration while j takes on the value of i + 1. Therefore, when i equals 5, j will equal 6 due to j = i + 1, which satisfies j < 5 condition for termination of the loop .

The compareTo() method compares lexicographically two strings unwrapped from their first non-matching character. It returns zero if both strings are equal, a value less than zero if the invoking string precedes the string being compared, and a value greater than zero if it follows the string being compared .

In Java, the switch statement’s controlling variable can only be of the data types byte, short, char, and int. Using other types like float or long would lead to a compilation error as these are not supported by switch .

In a switch statement, each case must have a unique constant value to prevent ambiguity and ensure correct execution flow within the switch block. Identical case constants would cause a logical error since the switch wouldn't be able to distinctly match the input value with the corresponding case .

The goto statement is not used in Java because it complicates code readability and maintenance by allowing arbitrary jump in code, which can lead to spaghetti code. Instead, Java provides structured statements like loops, switch, and try-catch blocks to control flow, ensuring more maintainable and understandable code .

The concat() method combines the string upon which it is called with another string, forming a new string without altering the original strings. For example, the result of concatenating "one" with "two" is "onetwo" .

A do-while loop should be chosen over a while loop when the code block inside the loop needs to be executed at least once, irrespective of the loop condition, since the do-while loop evaluates its condition after executing the loop body .

A switch statement can only test for equality between a variable and its constant cases, whereas an if statement can evaluate any type of boolean expression .

1. Which of these selection statements test only for equality?
a) if
b) switch
c) if & switch
d) none of the mentioned
View A
Answer: a
Explanation: None.
4. Which of these jump statements can skip processing remainder of code in its body for a partic
{
        public static void main(String args[])
        {
            int var1 = 5; 
            int var2 = 6;
class comma_operator 
    {
        public static void main(String args[]) 
        {    
             int sum = 0;
{
        public static void main(String args[]) 
        {        
             int x = 2;
             int y = 0;
output:
$ javac jump_statments.java
$ java jump_statments
1 3 5 7 9
9. What is the output of this program?
class Output 
{
Answer: d
Explanation: Every final variable is compile time constant.
10. What is the output of this program?
    class Outpu
}
a) 5 10
b) 10 5
c) 5
d) 10
Answer: d
Explanation: b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of i
a) 1 2
b) 2 3
c) 1 3
d) 3
View Answer
Answer: d
Explanation: Since the first if condition is not met, control would not go in
a) do statement executes the code of a loop at least once
b) do statement does not get execute if condition is not matched in

You might also like