Output of Java Programs | Set 43 (Conditional statements & Loops)
Last Updated :
29 Sep, 2017
Prerequisite: Decision Control and
Loops
1. What will be the output of the following program?
JAVA
class Test {
public
static void main(String[] args)
{
int i = 0, j = 9;
do {
i++;
if (j-- < i++) {
break;
}
} while (i < 5);
System.out.println(i + "" + j);
}
}
Options:
1.44
2.55
3.66
4.77
The answer is option (3)
Explanation : In the above program, we have to specially take care about the break statement. The execution of the program is going as usual as the control flow of do-while loop but whenever compiler encountered break statement its control comes out from the loop.
2. What will be the output of the following program?
JAVA
class Test {
public
static void main(String[] args)
{
int j = 0;
do
for (int i = 0; i++ < 1;)
System.out.println(i);
while (j++ < 2);
}
}
Options:
1. 111
2. 222
3. 333
4. error
The answer is option (1)
Explanation : As we all know that curly braces are optional in do and for loop. But the only criteria is if we declare a statement without curly statement, then the statement should not be declarative.
3. What will be the output of the following program?
JAVA
class Test {
static String s = "";
public
static void main(String[] args)
{
P:
for (int i = 2; i < 7; i++) {
if (i == 3)
continue;
if (i == 5)
break P;
s = s + i;
}
System.out.println(s);
}
}
Options:
1. 32
2. 23
3. 24
4. 42
The answer is option (3)
Explanation : In the above example, when the first for loop is executed then it holds the value of i as 2. As long as the i value is 2, the loop will not execute the if condition and will be directly as s=s+i. Here s stores the value in a string format. Next time when s=s+i is executed, the i value becomes 4. Both these values are stored in the s in the form of a string.
4. What will be the output of the following program?
JAVA
class Test {
public
static void main(String[] args)
{
int x = 10;
if (++x < 10 && (x / 0 > 10)) {
System.out.println("Bishal");
} else {
System.out.println("GEEKS");
}
}
}
Options:
1.Compile time error
2. RuntimeException:ArithmeticException: / by zero
3. Bishal
4. GEEKS
The answer is option (4)
Explanation : In the above program we are using && operator (short-circuit operator). Whenever we use && operator then if the first condition is false then the control does not go to the 2nd condition whether it is true or false. In the above program, the first condition in the if block is not true that's why the else part is executed.
5. What will be the output of the following program?
JAVA
class Test {
public
static void main(String[] args)
{
final int a = 10, b = 20;
while (a > b) {
System.out.println("Hello");
}
System.out.println("GEEKS");
}
}
Options:
1. Compile time error
2. GEEKS
3. Hello
4. No Output
The answer is option (1)
Explanation: In the above program, we declare two variables as
final. In the while loop, it always returns false and the control does not go inside while loop and it does not get the chance in the entire program. Thats why we will get compile time error saying error: unreachable statement.
Similar Reads
Output of Java Programs | Set 36 (do-while loop) Prerequisite : while loop in Java 1. What will be the output for the following program? JAVA class Test { public static void main(String[] args) { do while (true) System.out.println("HELLO"); while (false); } } Options: 1. HELLO 2. Compile time error 3. HELLO (infinitely) 4. No Output The
3 min read
Output of Java Programs | Set 40 (for loop) Prerequisite : Loops in Java 1. what will be the output of the following program? JAVA public class Test { public static void main(String[] args) { for (int i = 0; i < 10; i++) int x = 10; } } Options: 1. No Output 2. 10 3. Compile time error 4. 10 (10 times) The answer is option (3) Explanation:
3 min read
Output of Java Programs | Set 53 (String Comparison) Prerequisite : String Comparison in Java 1. What should be the output of this program? Java class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } a) tru
3 min read
Output of Java Programs | Set 45 (static and instance variables) Prerequisite : static and instance variables Question 1. What is the output of this question? JAVA class Test1 { int x = 10; public static void main(String[] args) { Test1 t1 = new Test1(); Test1 t2 = new Test1(); t1.x = 20; System.out.print(t1.x + " "); System.out.println(t2.x); } } Optio
3 min read
Output of Java Programs | Set 33 (Collections) Prerequisite: Java - Collections 1. What is the output of following Java Program? Java import java.util.ArrayList; class Demo { public void show() { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(7); list.add(1); for (int number : list) { System.out.print(numbe
3 min read
Output of Java Programs | Set 52 (Strings Class) Prerequisite : Basics of Strings class in java 1. What is the Output Of the following Program Java class demo1 { public static void main(String args[]) { String str1 = "java"; char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; String str2 = new
5 min read
Output of Python Programs | Set 23 (String in loops) Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? Python3 my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i ⦠g e e k s f o r g e e k s
2 min read
Output of Java Programs | Set 35 (Decision Making) Prerequisite: Decision Making in JavaPredict the Output of the following programs1. What will be the output of the following program?JAVA public class Test { public static void main(String[] args) { int x = 10; if (x) { System.out.println("HELLO GEEKS"); } else { System.out.println("BYE"); } } } Opt
6 min read
Output of Java Programs | Set 54 (Vectors) Prerequisite : Vectors in Java Basics 1. What is the Output Of the following Program Java import java.util.*; class demo1 { public static void main(String[] args) { Vector v = new Vector(20); System.out.println(v.capacity()); System.out.println(v.size()); } } Output: 20 0 Explanation: function - int
6 min read
Output of Java Programs | Set 38 (Arrays) Prerequisite : Arrays in Java Question 1. What is the output of this question JAVA class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; System.out.print(arr[-2]); } } Option A) 11 33 B) Error C) exception D) 11 -33 Output: C Explanation : We will get java.lang.ArrayInde
3 min read