edited by
1,431 views
0 votes
0 votes

What is the output of the following $C$ program?

# include <stdio.h>
main ()
{
    int i, j, x=0;
    for (i=0; i<5; ++i)
    for (j=0; j<i; ++j)
    {
    x+=(i+j-1);
    break;
    }
printf(“%d”, x);
}
  1. $6$
  2. $5$
  3. $4$
  4. $3$

 

edited by

3 Answers

5 votes
5 votes

$\underline{\textbf{Answer:}\Rightarrow}\;1)\;6$

$\underline{\textbf{Explanation:}\Rightarrow}$

$\color{magenta}{\text{Key points:}}$

  • When the $\mathbf {break}$ keyword is used in a loop, then after the execution of the 
    $\mathbf{break}$ statement in the loop, the control will come out of $\textbf{that loop only}$ and 
    nothing is executed after the $\color{black}{\mathbf {break}}$ statement.
  • In this question, the $\mathbf{j-loop}$ will be executed only once as there is $\textbf{break}$ in it.
    So, totally $\mathbf j$ will be executed for $\mathbf {j = 0\; (only)}$ and it will only be executed
    $\mathbf{5\; times}$ which is controlled by $\mathbf i$. $[\text{Here $\mathbf{i < 5}$, so $\mathbf 5$ times it will be executed}]$.

$\color {blue}{\textbf{Round 1:}}$

$\underline{\textbf{Initialization:}}$

values of $\textbf{i = 0, j = 0,}$ and $\textbf{x = 0;}$


As $\mathbf {j = 0,\;i=0}$ and $\mathbf{j \nless i}\;\;\text{So, nothing will be printed here.}$ $\bbox[lightgreen,5px,border: 2px solid red]{\;\;\;}$

$\color {blue}{\textbf{Round 2:}}$
$\textbf{i = 1; j = 0,} $ and $\bbox[lightgreen,5px,border: 2px solid red]{\mathbf x = 0}$

$\color {blue}{\textbf{Round 3:}}$
$\mathbf{i = 2; j = 0;} $ and $\bbox[lightgreen,5px,border: 2px solid red]{\mathbf x = 1}$

$\color {blue}{\textbf{Round 4:}}$
$\textbf{i = 3, j = 0,}$ and $\bbox[lightgreen,5px,border: 2px solid red]{\mathbf x = 3}$

$\color {blue}{\textbf{Round 5:}}$
$\mathbf{i = 4, j = 0,}$ and $\bbox[lightgreen,5px,border: 2px solid red]{\mathbf x = 6}$

$\therefore\mathbf{\bbox[lightblue,5px, border: 2px solid red] {\mathbf x = 6}}$ is the correct answer.


$\underline{\textbf{Output:}\Rightarrow}$

edited by
0 votes
0 votes
Since break statement is there for any value of i (apart form 0 as for i=0 j is initialized to 0 for which $\mathbf{j \nless i}$) the inner loop's statement will be executed only once. so, answer will be

$0\ (for\  i=1\ j=0)+1\ (for\ i=2\  j=0) + 2\ (for\ i=3\  j=0)+3\ (for\ i=4\  j=0) = 6$
0 votes
0 votes

Given x=0

The first for loop will execute for 4 times.

Also from the 2nd for loop, we know j will always equal zero as there is a break statement.

Simplifying x+=(i+j-1)  $\Rightarrow$x=x+(i+j-1)

When i=0, j=0 , x=0 because the condition in the 2nd for loop will become false.

 i=1, j=0, x=0 (x=0+1+0-1)

i=2, j=0, x=1 (x=0+2+0-1)

i=3, j=0, x=3 (x=1+3+0-1)

i=4, j=0, x=6 (x=3+4+0-1)

So ANS is option(A) 6.

 

Related questions

1 votes
1 votes
2 answers
1
4 votes
4 votes
6 answers
3
soujanyareddy13 asked May 12, 2021
1,862 views
The Boolean expression $AB+A \overline{B}+\overline{A}C+AC$ is unaffected by the value of the Boolean variable _________.$A$$B$$C$$A, B$ and $C$