2. Jumping Statements in C
• break is a keyword of C language.
• It is used to transfer the control of program out of switch or looping
statement.
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
break;
printf(“n%d”,i);
}return(0);
}
2
3. continue
• continue is a keyword of C language.
• It is used with looping statements. It is reverse of break statement.
• The continue statement forces the loop to continue or execute the next
iteration.
• When the continue statement is executed in the loop, the code inside the
loop following the continue statement will be skipped and the next
iteration of the loop will begin.
• #include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
continue;
printf(“n%d”,i);
}return(0);
} 3
4. goto
• goto is a keyword of C language.
• It is used to transfer the control of program from one part of program
to another part of program.
• It can be used anywhere in a program.
• goto statement can be used in two ways:
• Forward goto
• In forward goto statement, the label exists after the goto statement
• Backward goto
• In backward goto statement, the label exists before the goto statement.
4
6. Revision Questions
Q1 What is the output of the following source code?
#include<stdio.h>
int main()
{
int n;
for (n = 9; n!=0; n--)
printf("n = %d", n--);
return 0;
}
6
7. Revision Questions
Q2 What is the output of the following source code?
#include <stdio.h>
int main()
{
int i = 0;
switch (i)
{
case '0': printf("CU");
break;
case '1': printf(“AU");
break;
default: printf("CU-AU");
}
return 0;
7
8. Revision Questions
Q3 What is the output of the following source code?
#include <stdio.h>
int main()
{
int i;
for (i = 1; i != 10; i += 2)
printf(“ MST 2 ");
return 0;
}
8
9. Revision Questions
Q5 find the output
#include<stdio.h>
int main()
{
int i = -5;
while (i <= 5)
{
if (i >= 0)
break;
else
{
i++;
}
printf(“ C PROGRAMMING");
}
return 0;
} 9
10. Revision Questions
Q4 Predict the output of the below program:#include <stdio.h>
int main()
{
int i = 3;
switch(i)
{
printf("Outside ");
case 1: printf(“Case 1");
break;
case 2: printf(“case 2");
break;
defau1t: printf(“default");
}
return 0;
}
10
11. Revision Questions
Q6 #include <stdio.h>
int main()
{
int i = 3;
while (i--)
{
int i = 100;
i--;
printf("%d ", i);
}
return 0;
}
11
12. Revision Questions
Q7 #include <stdio.h>
int main()
{
int x = 3;
if (x == 2);
x = 0;
if (x == 3)
x++;
else x += 2;
printf("x = %d", x);
return 0;
}
12
13. Revision Questions
Q 8 #include<stdio.h>
int main()
{
int a = 5;
switch(a)
{
default:
a = 4;
case 6:
a--;
case 5:
a = a+1;
case 1:
a = a-1;
}
printf("%d n", a);
return 0;
} 13
14. Revision Questions
Q9 What is the output of C Program.?
int main()
{
while(true)
{
printf("RABBIT");
break; }
return 0;
}
14
15. Revision Questions
Q10 What is the output of C Program.?
int main()
{ int a=5;
while(a==5)
{
printf("RABBIT");
break;
}
return 0;
}
15
17. What will be printed when this program is
executed?
int f(int x)
{
if(x <= 4)
return x;
return f(--x);
}
void main()
{
printf("%d ", f(7));
}
17
18. Revision Questions
Q11 What is the output of C Program.?
int main()
{ int a=5;
while(a=123)
{
printf("RABBITn");
break;
}
printf("GREEN");
return 0; }
18
19. Revision Questions
Q 12 What is the output of C Program.?
int main()
{
int a=5;
while(a >= 3);
{
printf(“TEDDYn");
break;
}
printf(“BEAR");
return 0;
}
19
20. Revision Questions
Q14 What is the output of C Program.?
int main() {
int k;
for(printf(“Hello "); printf(“CSE "); printf(“STUDENTS "))
{
break;
}
return 0;
}
20
21. Revision Questions
Q15 What will be the output of the following code?
#include <stdio.h>
int main()
{
int i = 0, j = 0;
while (i<5,j<10)
{
i++;
j++;
}
printf("%d %d", i, j);
}
21
22. Revision Questions
Q16 What will be the output of the following code?
int main()
{
int a = 0;
while(a++)
{
printf(“C programming");
}
return 0;
}
22
23. Revision Questions
Q17 What will be the output of the following code?
int main()
{
while(1);
{
printf("C Programming");
}
return 0;
}
23
24. Revision Questions
Q18 What will be the output of the following code?
int main()
{
int i,j,count;
count=0;
for(i=0; i<5; i++);
{
for(j=0;j<5;j++);
{
count++;
}
}
printf("%d",count);
return 0;
}
24
25. Revision Questions
Q19 What will be the output of the following code?
int main()
{
int i,j,k,count;
count=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
count++;
}
}
printf("%d",count);
return 0;
}
25
26. Revision Questions
Q20 What will be the output of the following code?
int main()
{
int i,j;
for(i = 0,j=0;i<5;i++)
{
printf("%d%d--",i,j);
}
return 0;
}
26
27. Revision Questions
Q 21 What will be the output of following program ?
#include <stdio.h>
void main()
{
char cnt=0;
for(;cnt++;printf("%d",cnt)) ;
printf("%d",cnt);
}
27
28. Revision Questions
Q 24 What will be the output of following program ?
#include <stdio.h>
void main()
{
int tally;
for(tally=0;tally<10;++tally)
{
printf("#");
if(tally>6)
continue;
printf("%d",tally);
}
}
28
29. Revision Questions
Q 25 What will be the output of following program ?
#include <stdio.h>
void main()
{
int cnt=1;
do
{
printf("%d,",cnt);
cnt+=1;
}while(cnt>=10);
printf("nAfter loop cnt=%d",cnt);
printf("n");
}
29
30. Revision Questions
Q 27 What will be the output of following program ?
int main()
{
int a = 0;
while(a++);
{
printf("Cpp");
}
return 0;
}
30
31. Revision Questions
Q 28 What will be the output of following program
int main()
{
int i;
for(i = 0,i<5,i++)
{
printf("Hello");
}
return 0;
}
31
32. Revision Questions
Q 29 What will be the output of following program ?
#include <stdio.h>
void main()
{
int k = 0;
for (k)
printf("Hello");
}
32
33. Revision Questions
Q 30 What will be the output of following program ?
#include <stdio.h>
int main()
{
int i = 0;
for (; ; ;)
printf("In for loopn");
printf("After loopn");
}
33
34. Revision Questions
Q 31 What is output of below program?
int main()
{
for(; ;);
for(; ;);
printf("Hello");
return 0;
}
34
35. What is an Array in C language.?
A) A group of elements of the same data type.
B) An array contains more than one element
C) Array elements are stored in memory in continuous or contiguous
locations.
D) All the above.
35
36. An array index starts with value
A) 0
B) -1
C) 1
D) 2
36
37. int main()
{
int a[ ];
a[4] = {1,2,3,4};
printf("%d", a[0]);
}
a) 1
b) 2
c) 3
d) Compiler Error
37
38. If the two strings are identical, then strcmp() function returns
A. -1
B. 1
C. 0
D. None
38
39. Base address of an array represents the
a) Address of last element of an array
b) Address of second element of an array
c) Address of first element of an array
d) Number of elements
39
40. Array elements are always stored in ________ memory locations.?
a) Sequential
b) Random
c) Sequential and Random
d) None of the above
40
41. What is the output of this program?
void main()
{
int a[8] = {1,2,3,4,5};
printf("%d", a[5]);
}
A. 5
B. 6
C. 0
D. Garbage Value
41
42. #include <stdio.h>
int main()
{
char s1[] = "Hello";
char s2[] = "Hello";
if(s1 == s2)
printf("Same");
else
printf("Not Same");
return 0;
}
• A. Not Same
• B. Same
• C. Compilation Error
• D. None of the above
42
43. What is the output of this program?
#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
43
44. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%sn", strcpy(str2, strcat(str1, str2)));
return 0;
}
44
47. What will be the output of the following code?
void main()
{
int a[10];
printf("%d %d", a[-1], a[12]);
}
47
48. Which of the following correctly accesses the seventh element stored
in arr, an array with 100 elements?
A. arr[6]
B. arr[7]
C. arr{6}
D. arr{7}
E. None of these
48
49. • Which of the following function is more appropriate for reading in a
multi-word string?
A. scanf()
B. printf()
C. gets()
D. puts()
49
50. REVISION Questions
Predict output of following program
int main()
{
int i;
int arr[5] = {5};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
50
51. REVISION Questions
Q7 What is the output
int main()
{
int bus[3][] = {6,5,4,3,2,1};
printf("%d %d", bus[0][0], bus[2][1]);
return 0;
}
51
57. REVISION Questions
Find the output
void main()
{
int a;
printf("COUNT=");
a=show();
printf("%d", a);
}
int show()
{
return 15;
return 35;
}
57
58. Rohan writes the code for a function that computes the sum of n natural
numbers of the inputted number n.
int sum_n(int n)
{
if(n==1)
return 1;
else
missing statement
}
Fill in the missing statement.
58
59. • Sam is writing a code which has a function which calls itself. Which
programming concept is Sam using?
• The values passed to a function at the time of function calling are
knows as______________
• If we have to write a program to print first n natural ,which loop is
ideal?
59
60. Predict the Output of following C program ,if line 2 will be removed?
#include<stdio.h>
int add(int,int);//line 2
int add(int a,int b)
{
int sum=a+b;
return sum;
}
int main()
{
printf("%d",add(1,2));
return 0;
}
60
61. REVISION Questions
Q2 Find the output
int main()
{
int a[][] = {{1,2},{3,4}};
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
printf("%d ", a[i][j]);
return 0;
}
61
62. REVISION Questions
Q4 What is the output ?
int main()
{
char marks={'A','B','C'};
printf("%c", marks[0]);
}
62
65. Revision Questions
Q 23 What will be the output of following program ?
#include < stdio.h >
int main()
{
int tally=0;
for(;;)
{
if(tally==10)
break;
printf("%d ",++tally);
}
return 0;
}
65
66. What is the output of C Program.?
int main()
{
int k, j;
for(k=1, j=10; k <= 5; k++)
{
printf("%d ", (k+j));
}
return 0;
}
66
67. find the output
#include<stdio.h>
int main()
{
int i = -5;
while (i <= 5)
{
if (i >= 0)
break;
else
{
i++;
continue;
}
printf(“ C PROGRAMMING");
} return 0; }
67
68. Revision Questions
Q 22 What will be the output of following program ?
#include <stdio.h>
void main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i==5)
goto print;
i++;
}
}
fun()
{
print:
printf("includehelp.com");
}
68
69. What is the output of C Program.?
int main()
{
int a=32;
do
{
printf("%d ", a);
a++;
if(a > 35)
break;
}while(1);
return 0;
}
69
71. Let x be an array. Which of the following operations is/are illegal?
a) ++x
b) x+1
c) x++
d) x*2
71
72. What is the output of this program?
#include <stdio.h>
int main()
{
int arr[5] = {1,2,3,4,5};
int p, q, r;
p = ++arr[1];
q = arr[1]++;
r = arr[p++];
printf("%d, %d, %d", p, q, r);
return 0;
}
72
74. # include <stdio.h>
int main()
{
char str1[] = “Chandigarh";
char str2[] = {‘c’,’h’,’a’,’n’,’d’,’I’,’g’,’a’,’r’,’h’};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1 = %d, n2 = %d", n1, n2);
return 0;
}
74
75. • Which loop is faster in C Language, for, while or Do While.?
• a) for
• b) while
• c) do while
• d) All work at same speed
75
76. Revision Questions
Q 26 What will be the output in given input?
• When, input: 'a' and 1
int main()
{
int n,i=0;
while(scanf("%d",&n)==1)
{
printf("ENDn");
}
return 0;
}
76
Editor's Notes
Infinite loop
Cu AU
INFINITE LOOP
C PROGRAMMING 5 times
Nothing gets printed
99 99 99
X=2
5
COMPILE TIME ERROR
RABBIT
RABBIT
GREEN
INFINITE LOOP WITH NO ANSWER
HELLO CSE
10 10
Nothing will be printed
Infinite loop
1
25
00—10—20—30—40--
1Before entering into the for loop the CHECK CONDITION is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).
#0#1#2#3#4#5#6###
1,After loop cnt=2do while is an exit controlled loop, here loop body executed first, then condition will be checked.
Cpp
for loop should have semicolumn ; not comma , in its syntax. Using , instead of ; produces compilation error.
error
Hint:blank for loop with ; ; is always infinite loop. printf() will never executed in this program.
Explanation: The base address of s1 and s2 are checked which are not equal and hence the else part will execute.
A. 3, 2, 15
Explanation:- >> int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized toa[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25.>> int i, j, m; The variable i, j, m are declared as an integer type.>> i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2>> j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.>> m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)>> printf(“%d, %d, %d”, i, j, m); It prints the value of the variables i, j, mHence the output of the program is 3, 2, 15.
Hello World
5 0 0 0 0
ary[3][] has missing column count. Column count is a must for any multidimensional array.
Direct assignment not possible error
New
error
2
15
error
Compile time error
10
Here str[] has declared as 7 character array and into a 8 character is stored. This will result in overwriting of the byte beyond 7 byte reserved for '\0'.
1 2 3 4 5 6 7 8 9 10for(; ;) it is possible in c, there is no need to place condition with in the for(), you can place condition with in the body of the loop.
11 12 13 14 15
compiler error: Undefined label ‘print’ in function main.Labels have functions scope, in other words the scope of the labels is limited to functions. The label ‘print’ is available in function fun() Hence it is not visible in function main.
32 33 34 35
C. 4 3 4
1 2 0 0 7 8 9 0
No output and END
scanf function return integer value i.e. 1 if successful read data otherwise 0 in unsuccessfully read data.
1_ For, input ‘a’, scanf return 0 and while will not execute.
2_ For, input 1, scanf return 1 and while will be execute.