CP Tutorial - Answers
CP Tutorial - Answers
Tutorial 2
Logic:
We know that computer uses 2’s complement
to represent data. For example, if we have 1
byte, we can represent -128 to 127. If we add
1 to 127 we will get -128. That’s because 127
is 01111111 in binary. And if we add 1 into
01111111, we will get 10000000. 10000000 is
a negative number due to MSB being a sign
bit, sign bit as 1 denotes negative number and
is -128 in 2’s complement form.
Tutorial 3
Step 1: int i=-3, j=2, k=0, Step 1: int i=-3, j=2, k=0, Step 1: int x=12, y=7, z; here
m; here variable i, j, k, m are m; here variable i, j, k, m are variable x, y and z are declared
declared as an integer type declared as an integer type as an integer and
and variable i, j, k are and variable i, j, k are variable x and y are initialized
initialized to -3, 2, 0 initialized to -3, 2, 0 to 12, 7 respectively.
respectively. respectively.
Step 2: z = x!=4 || y == 2;
Step 2: m = ++i && ++j && ++k; Step 2: m = ++i || ++j && != and == has higher
becomes m = -2 && 3 && 1; ++k; here (++j && ++k;) this precedence than ||.
becomes m = TRUE && code will not get executed
TRUE; (Apply logic of short- because ++i has non-zero So, z = 12!=4 || 7 == 2;
circuit AND). value. (Apply logic of short- then z = (condition true) ||
circuit AND). (condition false); Hence it
Hence this statement becomes returns 1. So the value of z=1.
TRUE. So it returns '1'(one). becomes m = -2 || ++j && ++k;
Step 3: printf("z=%dn",
Hence m=1. becomes m = TRUE || ++j &&
z); Hence the output of the
++k; Hence this statement
program is "z=1".
Step 3: printf("%d, %d, %d, becomes TRUE. So it returns
%d\n", i, j, k, m); In the '1'(one). Hence m=1.
previous step the value of i,j,k
are incremented by '1'(one). Step 3: printf("%d, %d, %d,
%d\n", i, j, k, m); In the
Hence the output is "-2, 3, 1,
previous step the value of
1".
variable 'i' only incremented
by '1'(one). The variable j,k are
not incremented.
Hence the output is "-2, 2, 0,
1".
2, 3, 3 30 Compile Error
Step 1: int x=4, y, z; here Step 1: int k, num=30; here Because ++ or -- cannot be
variable x, y, z are declared as variable k and num are done on enum value.
an integer type and variable x is declared as an integer
initialized to 4. type and
variable num is initialized to
Step 2: y = --x; becomes y =
'30'.
3; because (--x) is pre-
decrement operator. Step 2: k = (num>5 ? (num
<=10 ? 100 : 200): 500); This
Step 3: z = x--; becomes z = 3;. statement does not affect the
In the next step output of the program.
variable x becomes 2, because Because we are going to print
(x--) is post-decrement the variable num in the next
operator. statement.
Tutorial 4
The output is
“Hi”
Tutorial 6
for loop contains semicolon at the end. while(‘1’ < ‘2’) -> here, ASCII value of 1 and ASCII
value of 2 is compared.
So, loop is terminated only when condition goes
false. ASCII of ‘1’ is 49 and ASCII value of ‘2’ is 50.
First time, i is 1, 1 <= 5, condition goes true, prints while(49 < 50) is true and there is no statement
character value of 65 due to statement ->printf that violates this condition
("\n%c", 65)
But there is no statement which increases the Hence, the output of the program is
value of i and hence, the condition never goes false “In while loop” is printed infinite times.
and hence, for loop is never terminated.
So, the output is an infinite loop where ASCII value Loop condition always get evaluated to true.
0 to 127, then -128 to -1, then back from 0 to 127 Also, at this point it increases i by one.
and its corresponding characters are printed in an
infinite manner. An increment expression i>0 has no effect on
value of i.so for loop get executed till the limit of
integer (ie. 65535)
x is incremented to 1, 1 <= 10, condition is true, Hence the output of the program is
if(x < 5) i.e. if(1 < 5), condition is true, continue is 1
executed, means it will skip the current iteration. 2
3
x is incremented to 2, 2 <= 10, condition is true, 4
if(x < 5) i.e. if(2 < 5), condition is true, continue is 5
executed, means it will skip the current iteration. 6
7
x is incremented to 3, 3 <= 10, condition is true, 8
if(x < 5) i.e. if(3 < 5), condition is true, continue is 9
executed, means it will skip the current iteration. 10
x is incremented to 4, 4 <= 10, condition is true,
if(x < 5) i.e. if(4 < 5), condition is true, continue is
executed, means it will skip the current iteration.
Tutorial 7
The output of the program is 3, 2, 15 The output of the program is Garbage value, 1,
2, 3, 4,
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr
is declared as an integer array with a size of 5 and First time value of i is 0.
it is initialized to while(0 < 5), condition is satisfied.
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 . arr[i] = ++i;
Step 2: int i, j, m; The variable i,j,m are declared as Assignment operator works from right to left.
an integer type. So first ++i will take place.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = ++i will give value as 1 and i is also incremented
2 and a[1] = 2 to 1.
Step 4: j = a[1]++; becomes j = 2++; Hence j = Now, arr[i] will take take value as arr[1] rather
2 and a[1] = 3. than arr[0], as i is incremented.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = So, arr[1] = 1, hence, arr[0] will not be holding
15 and i is incremented by 1(i++ means 2++ so i=3) a valid value and hence, will hold a garbage
Step 6: printf("%d, %d, %d", i, j, m); It prints the value.
value of the variables i, j, m Next time, while(1 < 5), condition goes true.
Hence, the output of the program is 3, 2, 15 Same procedure takes place, ++i will be 2 and I
is incremented to 2, then arr[2] = 2 and so on
till value of i reaches 5, when condition goes
false.
The sizeof function return the given variable. In C, if we initialize an array with fewer
Example: float a=10; sizeof(a) is 4 bytes members, all remaining members are
Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The automatically initialized as 0.
variable arr is declared as an floating point array
and it is initialized with the values. So, since arr[5] = {1} is written, means first
Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0])); element will be initialized to 1 and rest will be
The variable arr has 4 elements. The size of the taken as 0.
float variable is 4 bytes. So, arr[0] will be 1 and others will be 0.
Hence 4 elements x 4 bytes = 16 bytes Hence the output of the program is 1 0 0 0 0
sizeof(arr[0]) is 4 bytes
Hence 16/4 is 4 bytes
Hence the output of the program is 4
No, C does not perform bound checking, means if It will give compile error.
you try to print the value of array index outside the
range specified, it will not give error, instead will In statement, int a[ ][ ] = {{1,2},{3,4}};
give garbage value.
Arr[5] = {0} is written, that means all elements are Number of columns should be mentioned
initialized to 0. mandatorily as per the syntax rules.
So, arr[0] = 0, arr[1] = 0, arr[2] = 0, arr[3] = 0, arr[4]
= 0.
When, for loop is executed, it runs for i = 5 also, at
arr[5], the value is not assigned and hence it takes
garbage value.
The array is declared as arr[4] as value of size is 4. int a[2][3] = {1, 2, 3, 4, 5}; is defined.
Now, the array is not initialized with any value, so Means 2 rows and three columns.
it will take all garbage values in array. 1st row will have values 1 2 and 3
2nd row will have values 4 5 and 0 [0 because
Hence, arr[0] will be having garbage value, 6th value is not initialized, so will take 0]
garbage values are always positive values .
Using nested for loop, only array values are
So, arr[0] will be a true value, hence condition is printed.
satisfied and prints “Initialized to ZERO”.
Hence the output of the program is 1 2 3 4 5 0
Hence the output of the program is Initialized to
ZERO
Tutorial 8
The for loop will iterate for 26 times, for i = 0 to i = 25. Here, c is a string as character array is defined.
First time, arr[i] i.e. arr[0] = ‘A’ + i = ‘A’ + 0 = 65 + 0 And array c stores string value “A”.
[because 65 is ASCII value of ‘A’].
So, arr[0] = 65. At c[0] which is a character printed with %c, ‘A’ is
Then %d and %c values of arr[i] is printed, which is 65 A stored.
And so on till 26 characters are printed i.e. A to Z along
with corresponding ASCII values. And c string printed with %s, contains “A” only.
Hence, the output is Hence, the output is
65 A newline
66 B A
. A
.
.
89 Y
90 Z
As at the base address or starting of the string “Null” is placed, so while reading array if Null comes it assumes
that this is the end of array, so it terminates here only.
str = "Kanpur"; -> this kind of string assignment is p[i] = s[length — i];
not allowed. Hence, it gives error. For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0’
So p[0] becomes ‘\0’.
The statement str = "Kanpur"; generates an error.
We have to use strcpy function to copy a string. It doesn’t matter what comes in p[1], p[2]….. as p[0]
will not change for i > 0. Nothing is printed if we print
To remove error we have to change this a string with first character ‘\0’
statement str = "Kanpur"; to strcpy(str, "Kanpur");
Hence, the output is blank or null. Nothing is printed.
The program prints the string "anpur".
if(printf(“%s”,a)) counts the total characters in the
string a, since a is initialized to “\0”, hence, the total
characters will be 0.