in Technology by

What will be the output of the following Java program?

public class Test {  

public static void main(String[] args) {  

    int count = 1;  

    while (count <= 15) {  

    System.out.println(count % 2 == 1 ? "***" : "+++++");  

    ++count;  

        }      // end while  

    }       // end main   

 }  

a) 15 times ***

b) 15 times +++++

c) 8 times *** and 7 times +++++

d) Both will print only once

🔗Reference: stackoverflow.com

🔗Source: Java Interview Questions and Answers

1 Answer

0 votes
by

Answer: (c) 8 times *** and 7 times +++++

Explanation: In the above code, we have declared count = 1. The value of count will be increased till 14 because of the while (count<=15) statement. If the remainder is equal to 1 on dividing the count by 2, it will print (***) else print (+++++). Therefore, for all odd numbers till 15 (1, 3, 5, 7, 9, 11, 13, 15), it will print (***), and for all even numbers till 14 (2, 4, 6, 8, 10, 12, 14) it will print (+++++).

Hence, an asterisk (***) will be printed eight times, and plus (+++++) will be printed seven times.

Related questions

0 votes
    What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'] ... ;, ‘c’, [‘p’, ‘q’], ‘san}...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? def foo(x): x[0] = ['def'] x[1] = ['abc'] return id(x) q = ['abc ... (id(q) == foo(q)) a) Error b) None c) False d) True...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'] ... ;, ‘c’, [‘p’, ‘q’], ‘san}...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? def foo(x): x[0] = ['def'] x[1] = ['abc'] return id(x) q = ['abc ... (id(q) == foo(q)) a) Error b) None c) False d) True...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following program? public class MyFirst { public static void main(String[ ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    In Java, what will be the output of the following code and how? double x=2.9,y=2.5; (SOP is abbreviated form ... x),y)); Explain how Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
0 votes
    What is the output of the following Java program? class Test { int test_a, test_b; Test(int a, int b) { test_a = a; ... (); System.out.println(test.test_a+" "+test.test_b); } }...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    What is the output of the following Java program? class Test { int i; } public class Main { public static void main (String ... test = new Test(); System.out.println(test.i); } }...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    What is the output of the following Java program? public class Test { Test(int a, int b) { System.out.println("a = "+a+" b = "+ ... 10; byte b = 15; Test test = new Test(a,b); } }...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    What is the output of the following Java program? What is the output of the following Java program? class Test { public static ... ) { System.out.println("Hello Javatpoint"); } } }...
asked Nov 17, 2020 in Education by Editorial Staff
0 votes
    What is the output of the following Java program? class Test { public static void main (String args[]) { System.out.println(10 ... * 20); } } The output of the above code will be...
asked Nov 17, 2020 in Education by Editorial Staff
0 votes
    What is the output of the following Java program? class Test { public static void main (String args[]) { System.out.println(10 ... ; System.out.println("Javatpoint" + 10 + 20); } }...
asked Nov 17, 2020 in Education by Editorial Staff
0 votes
    Smriti wants to give output of a program in infinite loop which statement will you suggest to her to use? In qbasic Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    What would be the output of the following program segments? Give reasons to justify your answer. i) System.out.print( Sum ... + a ); Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
...