In: Computer Science
All QUESTIONS, PLEASE
5. What is the output of the following code:
int product = 1, i = 6;
while (i < 9)
{
product = product * i;
i++;
}
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
6. What is the output of the following code:
int product = 1, i = 6;
do
{
product = product * i;
i++;
} while (i < 9);
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
7. What is the output of the following code:
int product = 1, i = 6;
do
{
product = product * i;
i++;
} while (product < 9);
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
8. What is the output of the following code:
int product = 0, i = 6;
do
{
product = product * i;
i++;
} while (i < 9);
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
9. What is the output of the following code:
int i = 6;
for ( i = 0; i < 3; i++)
cout << “Hello” << endl;
cout << “Done!” << endl;
10. What is the output of the following code:
int i = 6;
for ( i = 0; i <= 3; i++)
cout << “Hello” << endl;
cout << “Done!” << endl;
11. What is the output of the following code:
int i = 6;
for ( i = 1; i < 3; i++)
cout << “Hello” << endl;
cout << “Done!” << endl;
12. What is the output of the following code:
int sum = 0, i = 14;
for ( i = 0; i < 5; i++)
{
sum = sum + i;
}
cout << “i is : ” << i << endl;
cout << “sum is : ” + sum);
13. What is the output of the following code:
int sum = 0, i = 14;
for ( i = 0; i < 40; i++)
{
if (i % 10 == 0)
sum = sum + i;
}
cout << “i is : ” << i << endl;
cout << “sum is : ” + sum);
no termination condition
14. What is the output of the following code:
int sum = 0, i = 14;
for ( i = 0; i < 10; i++)
{
i++;
sum = sum + i;
}
cout << “i is : ” << i << endl;
cout << “sum is : ” + sum);
15. What is the error in the following code:
int product = 1, i = 6;
do
{
product = product * i;
i++;
} while (product < 9)
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
5. What is the output of the following code:
int product = 1, i = 6;
while (i < 9)
{
product = product * i;
i++;
}
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
output:
i is 9
product is : 336 because it loops from i=6 to 9 and every iteration it multiplies i with product
sp product = 6*7*8 = 336
6. What is the output of the following code:
int product = 1, i = 6;
do
{
product = product * i;
i++;
} while (i < 9);
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
the output will remain same as previous question:
i is : 9
product is: 336 as we are just using do while loop instead of for loop
7. What is the output of the following code:
int product = 1, i = 6;
do
{
product = product * i;
i++;
} while (product < 9);
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
Output:
i is : 8
product is : 42
because when loop executes when i is 6 product is 6 loop continues, then i=7 product is 42 i becomes 8 but product<9 becomes false, so after loop terminates i is 8 and product is 42
8. What is the output of the following code:
int product = 0, i = 6;
do
{
product = product * i;
i++;
} while (i < 9);
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
output
i is : 9
product is : 0
because we have initialized product as 0 and it will remain 0 as 0x anything is 0
9. What is the output of the following code:
int i = 6;
for ( i = 0; i < 3; i++)
cout << “Hello” << endl;
cout << “Done!” << endl;
Ouptut:
Hello
Hello
Hello
Done!
because for loop runs 3 times and prints Hello on new line and then after for loop Done! is printed
10. What is the output of the following code:
int i = 6;
for ( i = 0; i <= 3; i++)
cout << “Hello” << endl;
cout << “Done!” << endl;
Output:
Hello
Hello
Hello
Hello
Done!
because this time runs loop from 0 to 3 which is 4 times it prints Hello and after loop it prints Done!
11. What is the output of the following code:
int i = 6;
for ( i = 1; i < 3; i++)
cout << “Hello” << endl;
cout << “Done!” << endl;
Output:
Hello
Hello
Done!
Because loop runs from 1 to 2 so it runs only 2 times and prints Hello and after loop it prints Done!
12. What is the output of the following code:
int sum = 0, i = 14;
for ( i = 0; i < 5; i++)
{
sum = sum + i;
}
cout << “i is : ” << i << endl;
cout << “sum is : ” + sum);
cout << “sum is : ” + sum); - if this is written correctly it will print the output otherwise it will be a compile time error as the syntax for cout is wrong.
Output:
i is: 5
Sum is: 10
because loop runs from 0 to 4 and sums all the numers which is 0+1+2+3+4 = 10
13. What is the output of the following code:
int sum = 0, i = 14;
for ( i = 0; i < 40; i++)
{
if (i % 10 == 0)
sum = sum + i;
}
cout << “i is : ” << i << endl;
cout << “sum is : ” + sum);
cout << “sum is : ” + sum); - if this is written correctly it will print the output otherwise it will be a compile time error as the syntax for cout is wrong.
Output:
i is: 40
Sum is: 60
because in every loop it will check if i is divisible by 10 if yes then it adds to sum so sum = 10 + 20 + 30 =60
14. What is the output of the following code:
int sum = 0, i = 14;
for ( i = 0; i < 10; i++)
{
i++;
sum = sum + i;
}
cout << “i is : ” << i << endl;
cout << “sum is : ” + sum);
cout << “sum is : ” + sum); - if this is written correctly it will print the output otherwise it will be a compile time error as the syntax for cout is wrong.
Output
i is : 10
Sum is : 25
because in every iteration i increases by 2 because we have written in loop i++ so
sum is 1 + 3 + 5 + 7 + 9 = 25
15. What is the error in the following code:
int product = 1, i = 6;
do
{
product = product * i;
i++;
} while (product < 9)
cout << “i is : ” << i << endl;
cout << “product is : ” << product << endl;
The error in the code is ; is missing after while conditon which is wrong syntax
if you like the answer please provide a thumbs up