int main
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 9; j++)
{
if(j >= i && j <= 10 - i) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Explanation:
The correct answer is option (a).
The condition j >= i && j <= 10-i ensures that stars get printed within a range that shrinks features from the edges.
2. In a nested loop used to print a pattern, what does the outer loop typically represent?
- Rows
- Columns
- Elements
- None of the above
Explanation:
The correct answer is option (a).
The outer loop is generally determine the number of rows in the given pattern.
3. What is the output of the following code snippet?
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("*");
}
printf("\n");
}
Explanation:
The correct answer is option (b).
The outer loop iterates three times; thus, for each iteration, the inner loop occurs and prints '***' before proceeding to the next line.
4. How many stars are printed by the following code?
for(int i=0; i<4; i++)
{
for (int j = 0; j <= i; j++)
{
printf("*");
}
printf("\n");
}
Explanation:
The correct answer is option (a).
The particular pattern that is printed is:
*
**
***
****
5. Which of the following codes will print a right-angled triangle?
for (int i = 1; i <= 5; i++)
{
for(int j=1; j<= i; j++)
{
printf("*");
}
printf("\n");
}
for (int i = 5; i > 0; i--) {
for (int j=1; j<=i; j++) {
printf("*");
}
printf("\n");
}
c. combination of a and b.
d. None of the above.
Explanation:
The correct answer is option (c).
The two parts of code print right-angled triangle patterns, although in different directions.
6. What will be the output of the following code snippet?
include <stdio.h>
int main
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 5; j++)
{
if(j < i) {
printf(" ");
} else {
printf("*");
}
}
printf("\n");
}
return 0;
}
Explanation:
The correct answer is option (a).
The inner loop prints spaces up to i, and then it prints stars.
7. What will be the value of Executed queries in the below code?
include <stdio.h>
int main
{
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 1 || i == j) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Explanation:
The correct answer is option (b).
The code prints the stars only when the row index is equal to the column index.
8. What pattern is produced by the following code snippet?
for (int i=0; i<4; i++)
{
for(int j = 0; j < 4 - i; ++j)
{
printf("*");
}
printf("\n");
}
Explanation:
The correct answer is option (a).
The inner loop limits the number of stars being printed with each execution of the outer loop.
9. What will be the output of the following code snippet?
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
{
if(j >= 6-i) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
Explanation:
The correct answer is option (a).
The condition stating j >= 6-i is used to print stars only when the value of j is greater than or equal to 6-i.