1.
What is the correct syntax for each loop?
Correct Answer
B. For(data_type variable:collection)
{body;}
Explanation
The correct syntax for each loop is "for(data_type variable:collection) {body;}". This syntax is commonly used in programming languages like Java and Python to iterate over elements in a collection, such as an array or a list. The "data_type" refers to the type of the elements in the collection, and "variable" is the name given to each element as it is being iterated over. The "body" represents the code block that will be executed for each iteration of the loop.
2.
Is following code snippet is correct?
1
2
for(int i=1; i<=10; i++)
System.out.println(i);
Correct Answer
A. Yes
Explanation
The code snippet is correct because it initializes a variable i to 1, and then runs a loop that executes as long as i is less than or equal to 10. In each iteration of the loop, it prints the value of i. This will result in printing the numbers from 1 to 10.
3.
What will be the output of the following program?
1
2
3
4
5
6
7
8
public class temp
{
public static void main(String agrs[])
{
for(int i=1; i<=10; i++);
System.out.print(i);
}
}
Correct Answer
C. Error
Explanation
The program will output "Error" because there is a syntax error in the for loop. The semicolon (;) after the for loop is causing the loop to terminate immediately, and the System.out.print(i) statement is outside of the loop. Therefore, the variable "i" is out of scope and cannot be accessed, resulting in an error.
4.
What will be the output of the following program?
1
2
3
4
5
6
7
8
9
public class temp
{
public static void main(String agrs[])
{
int i;
for(i=1; i<=10; i++);
System.out.print(i);
}
}
Correct Answer
B. 11
Explanation
The program will output 11. This is because the for loop is terminated by a semicolon after the condition, resulting in an empty loop body. As a result, the loop does not execute any statements and the value of i remains at 11, which is then printed.
5.
What will be the output of the following program?
1
2
3
4
5
6
7
8
9
public class temp
{
public static void main(String agrs[])
{
int x[]={1,2,3,4,5};
for(int i=0; i<x.length;i++)
System.out.print(x[i]);
}
}
Correct Answer
D. 12345
Explanation
The program creates an array called "x" with elements 1, 2, 3, 4, and 5. It then uses a for loop to iterate over the elements of the array and print each element. Therefore, the output of the program will be the numbers 1, 2, 3, 4, and 5 printed on separate lines.
6.
What will be the output of the following program?
1
2
3
4
5
6
7
8
public class temp
{
public static void main(String agrs[])
{
for(int i=1, int j=1; i<5 ; i++, j++)
System.out.print(i+""+j);
}
}
Correct Answer
D. Error
Explanation
The program will give an error because in the for loop declaration, you cannot declare multiple variables of different types using a comma. In this case, the variables i and j are both declared as int, but they are separated by a comma instead of a semicolon. This is not valid syntax in Java, hence the program will not compile and give an error.
7.
What will be the output of the following program?
1
2
3
4
5
6
7
8
9
public class temp
{
public static void main(String agrs[])
{
for(int i=1, j=1; i<5 ; i++, j++)
System.out.print(i+""+j);
}
}
Correct Answer
C. 11223344
Explanation
The program uses a for loop to iterate from 1 to 5. Inside the loop, it prints the values of both i and j concatenated together. Since i and j are both initialized to 1 and incremented by 1 in each iteration, the output will be 1122334455.
8.
Consider the given code snippet and select the ANS.
1
2
for(int i=1, j=1; i<5 ; i++, j++)
System.out.print(i+j);
Correct Answer
A. 2468
Explanation
The given code snippet is a for loop that initializes two variables i and j to 1. The loop runs as long as i is less than 5. In each iteration, it prints the sum of i and j. Since i and j both start at 1 and increment by 1 in each iteration, the output will be the sequence of numbers 2, 4, 6, and 8. Therefore, the correct answer is 2468.
9.
How many times "Hello world!" will be printed? (Consider the following code snippet).
1
2
3
4
5
6
7
8
int x = 2;
int y = 8;
while(x<(y+5))
{
System.out.println("Hello world!");
x+=2;
y-=2;
}
Correct Answer
A. 3
Explanation
The code snippet contains a while loop that will continue running as long as the condition x
10.
What will be the output of the following program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class temp
{
public static void main(String agrs[])
{
int loop;
for(loop=1; loop<=10; loop++)
{
loop*=2;
if(loop>12)
break;
}
System.out.println(loop);
}
}
Correct Answer
D. 14
Explanation
The program starts with initializing the variable loop to 1. Then, it enters a for loop where the value of loop is multiplied by 2 in each iteration. If the value of loop becomes greater than 12, the loop is terminated using the break statement. In this case, the loop terminates when the value of loop is 16. Therefore, the output of the program is 16.
11.
What would be the output of the following code snippet if variable a=10?
- if(a<=0)
- {
- if(a==0)
- {
- System.out.println("1 ");
- }
- else
- {
- System.out.println("2 ");
- }
- }
- System.out.println("3 ");
Correct Answer
D. 3
Explanation
The output of the code snippet will be "3". This is because the condition "a
12.
The while loop repeats a set of code while the condition is not met?
Correct Answer
B. False
Explanation
The while loop repeats a set of code while the condition is met, not while the condition is not met. Therefore, the correct answer is False.
13.
What is true about a break?
Correct Answer
B. Break halts the execution and forces the control out of the loop
Explanation
The statement "Break halts the execution and forces the control out of the loop" is true because when a break statement is encountered in a loop, it immediately terminates the loop and transfers control to the next statement after the loop. This allows the program to exit the loop prematurely based on a certain condition or criteria.
14.
What is true about do statement?
Correct Answer
A. Do statement executes the code of a loop at least once
Explanation
The do statement executes the code of a loop at least once, regardless of whether the condition is initially true or false. This is because the condition is checked at the end of the loop, after the code has been executed. Therefore, even if the condition is false from the beginning, the code will still be executed once before checking the condition again.
15.
Which of the following is used with the switch statement?
Correct Answer
C. Break
Explanation
The correct answer is "break" because it is used with the switch statement to terminate the execution of the switch block. When a break statement is encountered, the control is transferred to the next statement after the switch block. Without the break statement, the code would continue to execute the following case statements until a break statement is encountered or the end of the switch block is reached.
16.
What is the valid data type for variable “a” to print “Hello World”?
- switch(a)
- {
- System.out.println("Hello World");
- }
Correct Answer
D. Byte and char
Explanation
The switch statement in Java can only accept variables of type byte, char, short, or int as its argument. In this case, the variable "a" needs to be of type byte or char in order for the code to compile and print "Hello World".
17.
Which of the following is not a decision making statement?
Correct Answer
D. Do-while
Explanation
A do-while statement is not a decision-making statement because it is a looping statement that executes a block of code repeatedly until a certain condition is no longer true. It does not make a decision based on a condition like the other options. The if statement, if-else statement, and switch statement are all decision-making statements that determine the flow of the program based on a condition.
18.
Which of the following is not a valid jump statement?
Correct Answer
B. Goto
Explanation
The "goto" statement is not a valid jump statement in most modern programming languages. It allows the program to jump to a specific labeled line of code, which can lead to unstructured and hard-to-maintain code. The use of "goto" can make the program flow unpredictable and can cause difficulties in debugging and understanding the code. As a result, many programming languages have chosen to exclude the "goto" statement as a valid jump statement.
19.
From where break statement causes an exit?
Correct Answer
D. From innermost loops or switches
Explanation
The break statement causes an exit from the innermost loop or switch statement. This means that when a break statement is encountered within a loop or switch, the program will immediately exit that loop or switch and continue with the next statement outside of it. It will not terminate the entire program or exit from any other loops or switches that may be nested within.
20.
Which of the following is not a valid flow control statement?
Correct Answer
A. exit()
Explanation
The exit() function is not a valid flow control statement. It is a function used to terminate the program execution and exit the current process. Flow control statements are used to alter the flow of program execution based on certain conditions or criteria. The exit() function is typically used to indicate a successful termination or to handle exceptional cases, but it does not control the flow of program execution like break, continue, or return statements.
21.
Which of these selection statements test only for equality?
Correct Answer
B. Switch
Explanation
The switch statement is the only selection statement that tests only for equality. Unlike the if statement, which can test for various conditions using comparison operators, the switch statement evaluates a single expression and compares it to multiple case values to determine the appropriate block of code to execute. Therefore, the switch statement is the correct answer as it solely tests for equality.
22.
Which of these are selection statements in Java?
Correct Answer
A. If()
Explanation
The if() statement is a selection statement in Java. It allows the program to make decisions based on a certain condition. If the condition is true, the code inside the if() block will be executed, otherwise, it will be skipped. This statement is commonly used to implement conditional branching in programs, where different actions are taken depending on the outcome of a condition.
23.
Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
Correct Answer
A. Do-while
Explanation
The do-while loop is the correct answer because it is the only loop that guarantees the execution of the loop body at least once, even if the condition controlling the loop is initially false. This is because in a do-while loop, the condition is checked at the end of the loop iteration, so the loop body is always executed at least once before the condition is evaluated.
24.
Which of these jump statements can skip processing the remainder of the code in its body for a particular iteration?
Correct Answer
D. Continue
Explanation
The jump statement "continue" can skip processing the remainder of the code in its body for a particular iteration. When "continue" is encountered in a loop, it immediately jumps to the next iteration without executing the remaining code in the loop for that iteration. This allows for skipping certain iterations based on certain conditions, while still continuing the loop.
25.
Which of this statement is incorrect?
Correct Answer
B. Two case constants in the same switch can have identical values
Explanation
The statement "two case constants in the same switch can have identical values" is incorrect. In a switch statement, each case constant must have a unique value. If two case constants have the same value, it will result in a compilation error.
26.
What will be the output of the following Java program?
- class selection_statements
- {
- public static void main(String args[])
- {
- int var1 = 5;
- int var2 = 6;
- if ((var2 = 1) == var1)
- System.out.print(var2);
- else
- System.out.print(++var2);
- }
- }
Correct Answer
B. 2
Explanation
The output of the program will be 2. This is because the if statement is checking if the value of var2 after assigning 1 to it is equal to var1. Since the assignment is successful, the condition evaluates to true and the code inside the if block is executed. Therefore, the value of var2, which is 1, is printed.
27.
What will be the output of the following Java 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;
- System.out.println(sum);
- }
- }
Correct Answer
B. 6
Explanation
The program initializes two variables, i and j, to 0. It then enters a for loop that continues as long as both i and j are less than 5. In each iteration of the loop, i is incremented by 1 and j is set to the value of i + 1. The sum variable is incremented by the value of i in each iteration. After the loop, the value of sum is printed, which is 6.
28.
What will be the output of the following Java 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
- System.out.print(y + " ");
- }
- }
- }
Correct Answer
C. 1 3 5 7 9
Explanation
The program starts with initializing two variables, x = 2 and y = 0. Then, it enters a for loop that runs as long as y is less than 10. Inside the loop, it checks if y is divisible by x (2) using the condition y % x == 0. If it is true, the continue statement is executed, which skips the rest of the code inside the loop and moves to the next iteration. If y is equal to 8, the break statement is executed, which terminates the loop. Otherwise, if none of the conditions are met, the value of y is printed. In this case, the values printed are 1, 3, 5, 7, and 9.
29.
What will be the output of the following Java program?
- class Output
- {
- public static void main(String args[])
- {
- final int a=10,b=20;
- while(a<b)
- {
-
- System.out.println("Hello");
- }
- System.out.println("World");
-
- }
}
Correct Answer
D. Compile time error
Explanation
The given program will result in a compile time error. This is because the condition in the while loop, "a
30.
What will be the output of the following Java program?
- class Output
- {
- public static void main(String args[])
- {
- int a = 5;
- int b = 10;
- first:
- {
- second:
- {
- third:
- {
- if (a == b >> 1)
- break second;
- }
- System.out.println(A.;
- }
- System.out.println(B.;
- }
- }
- }
Correct Answer
D. 10
Explanation
The output of the program will be "10". This is because the if statement checks if the value of variable "a" is equal to the value of variable "b" shifted right by 1 bit. Since the value of "a" is 5 and the value of "b" is 10, the condition is not true and the break statement is not executed. Therefore, the program continues to the next line which prints the value of "b", which is 10.
31.
Which of these have highest precedence?
Correct Answer
A. ()
Explanation
Parentheses have the highest precedence in this case. This means that any expression enclosed in parentheses should be evaluated first before any other operation is performed.
32.
What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
Correct Answer
C. Boolean
Explanation
The expression1 in the given line should evaluate to a boolean value. This is because the ternary operator requires the expression1 to be a condition that evaluates to either true or false. The value of expression2 will be returned if the condition is true, otherwise the value of expression3 will be returned.
33.
What is the value stored in x in the following lines of Java code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
Correct Answer
D. 8
Explanation
The value stored in x is 8. In the given code, the variables x, y, and z are declared as integers. Initially, x is assigned a value of 0 and y is assigned a value of 1. Then, the statement "x = y = z = 8" is executed. This statement assigns the value 8 to z, then assigns the value of z (which is 8) to both y and x. Therefore, the final value stored in x is 8.
34.
What is the order of precedence (highest to lowest) of following operators?
1. &
2. ^
3. ?:
Correct Answer
A. 1 -> 2 -> 3
Explanation
The order of precedence of operators is determined by the hierarchy of operations they perform. In this case, the ^ operator has the highest precedence, followed by the && operator, and finally the ?: operator. This means that the ^ operator will be evaluated first, then the && operator, and finally the ?: operator.
35.
Which of these statements are incorrect?
Correct Answer
C. Division operator, /, has higher precedence than multiplication operator
Explanation
The given statement is incorrect because in mathematics, multiplication and division have the same precedence. This means that they are performed from left to right in the order they appear in an expression. Therefore, neither the division operator nor the multiplication operator has higher precedence over the other.
36.
What will be the output of the following Java code?
- class operators
- {
- public static void main(String args[])
- {
- int var1 = 5;
- int var2 = 6;
- int var3;
- var3 = ++ var2 * var1 / var2 + var2;
- System.out.print(var3);
- }
- }
Correct Answer
C. 12
Explanation
Pre-Increment Operation: ++var2 increments var2 from 6 to 7 before it is used in the expression.
Multiplication and Division: The expression var2 * var1 / var2 involves multiplying the incremented var2 (which is now 7) by var1 (which is 5), resulting in 35. This result is then divided by var2 (still 7), yielding 5.
Addition: Finally, add the current value of var2 (which remains 7) to the result of the division, resulting in 5 + 7.
Thus, the full expression evaluates as:
7 * 5 / 7 + 7
35 / 7 + 7
5 + 7
12
37.
What will be the output of the following Java code?
- class operators
- {
- public static void main(String args[])
- {
- int x = 8;
- System.out.println(++x * 3 + " " + x);
- }
- }
Correct Answer
D. 27 9
Explanation
The code first increments the value of x by 1 using the pre-increment operator (++x). Then, it multiplies the incremented value of x by 3 and concatenates it with a space character. Finally, it prints the result, which is "27" followed by a space and the value of x (which is 9).
38.
What will be the output of the following Java code?
- class Output
- {
- public static void main(String args[])
- {
- int x=y=z=20;
-
- }
- }
Correct Answer
D. Compile time error
Explanation
The code will result in a compile time error because the variables x, y, and z are not declared before they are assigned the value of 20. Each variable should be declared separately before assigning a value to it.
39.
Which of these lines of Java code will give better performance?
1. a | 4 + c >> b & 7;
2. (a | ((( 4 * c ) >> b ) & 7 ))
Correct Answer
C. Both 1 & 2 will give equal performance
Explanation
Both 1 and 2 will give equal performance. The presence or absence of parentheses in the code does not affect the performance of the code. The performance of the code depends on the operations being performed and the efficiency of the underlying Java compiler and runtime environment.
40.
What will be the output of the following Java program?
- class Output
- {
- public static void main(String args[])
- {
- int a,b,c,d;
- a=b=c=d=20
- a+=b-=c*=d/=20
- System.out.println(a+" "+b+" "+c+" "+d;
-
- }
- }
Correct Answer
C. A=20 b=0 c=20 d=1
Explanation
The given program will output a=20, b=0, c=20, and d=1. This is because of the order of operations in the expression a+=b-=c*=d/=20.
First, the value of d is divided by 20, resulting in d=1. Then, c is multiplied by the updated value of d, which is 1, resulting in c=20. Next, b is subtracted by the updated value of c, which is 20, resulting in b=0. Finally, a is incremented by the updated value of b, which is 0, resulting in a=20.
41.
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)
- System.out.print(var2);
- else
- System.out.print(++var2);
- }
- }
Correct Answer
B. 2
Explanation
The output of this program is 2. This is because in the if statement, the variable var2 is assigned the value 1 and then compared to var1. Since the assignment operation returns the value that was assigned, the condition becomes true and the code inside the if statement is executed, which prints the value of var2 (which is 1). However, in the else statement, the value of var2 is incremented by 1 before being printed, resulting in the final output of 2.
42.
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;
- System.out.println(sum);
- }
- }
Correct Answer
B. 6
Explanation
The program initializes two variables, i and j, to 0. It then enters a for loop that increments i and sets j to i + 1 on each iteration. The loop continues as long as both i and j are less than 5. Inside the loop, the value of i is added to the variable sum. After the loop completes, the value of sum is printed, which is 6. This is because the loop runs for 5 iterations, with i taking the values 0, 1, 2, 3, and 4. The sum of these values is 10, but since j is always one step ahead of i, the final value of sum is 6, not 10.
43.
What is the output of this program?
class jump_statements {
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
System.out.print(y + " ");
}
}
}
Correct Answer
A. 1 3 5 7
Explanation
This Java program demonstrates the use of continue and break statements within a for loop.
The continue statement skips the current iteration of the loop if the condition (y % x == 0) is true, meaning when y is even.
The break statement terminates the loop completely when y is equal to 8.
Therefore, the program prints only the odd numbers from 1 to 7, as 8 triggers the break statement, and the even numbers are skipped due to the continue statement.Sources and related content
44.
What is the output of this program?
- class Output {
- public static void main(String args[])
- {
- final int a=10,b=20;
- while(a<B.
- {
-
- System.out.println("Hello");
- }
- System.out.println("World");
-
- }
- }
Correct Answer
D. Compile time error
Explanation
The program will result in a compile time error. This is because there is a syntax error in the while loop condition. The variable "B" should be "b" to match the variable declaration.
45.
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;
- }
- System.out.println(A.;
- }
- System.out.println(B.;
- }
- }
-
}
Correct Answer
D. 10
Explanation
The output of the program is 10. This is because the if statement checks if the value of a is equal to the value of b shifted right by 1 (which is equivalent to dividing b by 2). Since b is 10 and 10 shifted right by 1 is 5, the condition is false and the code inside the if statement is not executed. Therefore, the program continues to the next line which is System.out.println(B.; and prints the value of b, which is 10.