Campus/Interview preparation with C language
Starting with Important topic of C Language :-
- Operators
- Decision Control
- Loops
- Function
- Recursion
- Arrays
- Strings
- Pointer
- source-www.mysirg.com
![]() |
campus/interview preparation |
Objective type questions or multiple choice questions (MCQ) are often asked in various exams, including job recruitment tests. It also helps in understanding language constructs, syntax and fundamental rules.click the link given in above list to go to that topic.
(a) 20
(b) 30
(c) Compiler Error
(d) Runtime Error
(a) 2 2
(b) 2 1
(c) 0 1
(d) 0 2
(a) 30
(b) 20
(c) Compiler
(d) Runtime
(a) Ibm
(b) Gis
(c) Pro
(d) ccenture
(a) 3
(b) 30
(c) 27
(d) 24
(a) Miye
(b) Iye
(c) Bye
(d) None
If this article is helpful to you then please like share and comments...thank you..
Pointers in C
1.
#include “stdio.h”
void fun(int *ptr)
{
*ptr = 30;
}
#include “stdio.h”
void fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int y = 20;
fun(&y);
printf(“%d”, y);
{
int y = 20;
fun(&y);
printf(“%d”, y);
return 0;
}
}
(a) 20
(b) 30
(c) Compiler Error
(d) Runtime Error
Answer
(b) The function fun() expects a pointer ptr to
an integer (or an address of an integer). It modifies the value at the address
ptr. The dereference operator * is used to access the value at an address. In
the statement ‘*ptr = 30’, value at address ptr is changed to 30. The address
operator & is used to get the address of a variable of any data type. In
the function call statement ‘fun(&y)’, address of y is passed so that y can
be modified using its address.
2.
int main()
{
char *ptr = “BcaKanpur”;
printf(“%c\n”, *&*&*ptr);
return 0;
}
int main()
{
char *ptr = “BcaKanpur”;
printf(“%c\n”, *&*&*ptr);
return 0;
}
(a) Compiler
Error
(b) Garbage Value
(c) Runtime Error
(d) B
(b) Garbage Value
(c) Runtime Error
(d) B
Answer
(d) The operator * is used for dereferencing and
the operator & is used to get the address. These operators cancel out
effect of each other when used one after another. We can apply them
alternatively any no. of times. In the above code, ptr is a pointer to first
character of string B. *ptr gives us B, &*ptr gives address of B,
*&*ptr again B, &*&ptr address of B, and finally *&*&ptr
gives ‘B’
3.
#include “stdio.h”
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf(“%d %d \n”, i, j);
getchar();
return 0;
}
#include “stdio.h”
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf(“%d %d \n”, i, j);
getchar();
return 0;
}
(a) 2 2
(b) 2 1
(c) 0 1
(d) 0 2
Answer
(d) See below f() with comments for explanation.
/* p points to i and q points to j */
void f(int *p, int *q)
{
p = q; /* p also points to j now */
*p = 2; /* Value of j is changed to 2 now */}
/* p points to i and q points to j */
void f(int *p, int *q)
{
p = q; /* p also points to j now */
*p = 2; /* Value of j is changed to 2 now */}
4.
#include “stdio.h”
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
++*p;
p += 2;
printf(“%d”, *p);
return 0;
}
#include “stdio.h”
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
++*p;
p += 2;
printf(“%d”, *p);
return 0;
}
(a) 2
(b) 3
(c) 4
(d) Compiler
(b) 3
(c) 4
(d) Compiler
Answer
(b) The expression ++*p is evaluated as “++(*p)”
. So it increments the value of first element of array (doesn’t change the
pointer p).
When p += 2 is done, p is changed to point to third element of array.
When p += 2 is done, p is changed to point to third element of array.
5.
void f(int* p, int m)
{
m = m + 5;
*p = *p + m;
return;
}
void main()
{
int i=5, j=10;
f(&i, j);
printf(“%d”, i+j);
}
void f(int* p, int m)
{
m = m + 5;
*p = *p + m;
return;
}
void main()
{
int i=5, j=10;
f(&i, j);
printf(“%d”, i+j);
}
(a) 10
(b) 20
(c) 30
(d) 40
(b) 20
(c) 30
(d) 40
Answer
(c) For i, address is passed. For j, value is
passed. So in function f, p will contain address of i and m will contain value
10. Ist statement of f() will change m to 15. Then 15 will be added to value at
address p. It will make i = 5+15 = 20. j will remain 10. print statement will
print 20+10 = 30.
6.
#include
“stdio.h”
void fun(int x)
{
x = 30;
}
void fun(int x)
{
x = 30;
}
int main()
{
int y = 20;
fun(y);
printf(“%d”, y);
return 0;
}
{
int y = 20;
fun(y);
printf(“%d”, y);
return 0;
}
(a) 30
(b) 20
(c) Compiler
(d) Runtime
Answer
(b) Parameters are always passed by value in C.
Therefore, in the above code, value of y is not modified using the function
fun(). So how do we modify the value of a local variable of a function inside
another function. Pointer is the solution to such problems. Using pointers, we
can modify a local variable of a function inside another function. See the next
question.
Note that everything is passed by value in C. We only get the effect of pass by reference using pointers.
Note that everything is passed by value in C. We only get the effect of pass by reference using pointers.
7.
#include “stdio.h”
int main()
{
static char *a[] = {“ibm”, “wipro”, “accenture”, “aegis”};
char **ptr[] = {a+3, a+2, a+1, a}, ***p;
p = ptr;
++p;
printf(“%s”, **p+1);
return 0;
}
#include “stdio.h”
int main()
{
static char *a[] = {“ibm”, “wipro”, “accenture”, “aegis”};
char **ptr[] = {a+3, a+2, a+1, a}, ***p;
p = ptr;
++p;
printf(“%s”, **p+1);
return 0;
}
(a) Ibm
(b) Gis
(c) Pro
(d) ccenture
Answer
(d) 1st line of the program makes an array of
char pointers(i.e strings in c++), having a[0] = “ibm” … etc.
2nd line create in another array having reversed order of strings in array ‘a'(i.e. the first string in this array is “aegis”).
3rd line making **p to point to the same address as **ptr is pointing to.
4th line increases the address of by one to which the **p is pointing to(i.e now **p is pointing to string “accenture”).
5th line dereferences the next address within the string which was pointing by **p(i.e firstly **p is now pointing to “accenture”).
2nd line create in another array having reversed order of strings in array ‘a'(i.e. the first string in this array is “aegis”).
3rd line making **p to point to the same address as **ptr is pointing to.
4th line increases the address of by one to which the **p is pointing to(i.e now **p is pointing to string “accenture”).
5th line dereferences the next address within the string which was pointing by **p(i.e firstly **p is now pointing to “accenture”).
8.
#include “stdio.h”
int main()
{
int a=3.2, *b, c;
b = &a;
printf(“%d\n”, a**b*a-*b);
return 0;
}
#include “stdio.h”
int main()
{
int a=3.2, *b, c;
b = &a;
printf(“%d\n”, a**b*a-*b);
return 0;
}
(a) 3
(b) 30
(c) 27
(d) 24
Answer
(d) i**j*i-*j
above line executed in following steps
1>a**b
3**3=9
2>a**b*a
9*3=27(9 from the previous step)
3>a**b*a-*b
27-*j=27-*j=27-3=24
above line executed in following steps
1>a**b
3**3=9
2>a**b*a
9*3=27(9 from the previous step)
3>a**b*a-*b
27-*j=27-*j=27-3=24
9.
#include “stdio.h”
int main()
{
int i=5, *j, *k;
j=&i; /* Assume address of i is 500 and integer is 4 byte size */
k=j;
*j++=*k++;
i++;
printf(“i=%d, j=%d, k=%d\n”, i, j, k);
return 0;
}
#include “stdio.h”
int main()
{
int i=5, *j, *k;
j=&i; /* Assume address of i is 500 and integer is 4 byte size */
k=j;
*j++=*k++;
i++;
printf(“i=%d, j=%d, k=%d\n”, i, j, k);
return 0;
}
(a) I=6,
j=502,k=502
(b) I=6,j=500,k=500
(c) I=6,j=504,k=504
(d) I=6,j=498,k=498
(b) I=6,j=500,k=500
(c) I=6,j=504,k=504
(d) I=6,j=498,k=498
Answer
(c )Here j=k; means both has same address i.e
500..ok…
now this expression *j++=*k++; means “whatever the the VALUE AT k++ ,assign it to the address of j++”
note:-here *j++ & *k++ both are post increment so…firstly
expression(*j++=*k++;) is performed.
so…value at k is assigned to j(i.e nothing but 5)
and after that both address is incremented by 4 bytes(according to
the question) so finally..
i++;
becomes 6 and j,k becomes 504,504
now this expression *j++=*k++; means “whatever the the VALUE AT k++ ,assign it to the address of j++”
note:-here *j++ & *k++ both are post increment so…firstly
expression(*j++=*k++;) is performed.
so…value at k is assigned to j(i.e nothing but 5)
and after that both address is incremented by 4 bytes(according to
the question) so finally..
i++;
becomes 6 and j,k becomes 504,504
10.
#include
int main()
{
char s[20] = “bye”;
char *const p=s;
*p=’Mi’;
printf(“%s\n”, s);
return 0;
}
#include
int main()
{
char s[20] = “bye”;
char *const p=s;
*p=’Mi’;
printf(“%s\n”, s);
return 0;
}
(a) Miye
(b) Iye
(c) Bye
(d) None
Answer
(b) As p is const variable pointer it cannot be
incremented or decremented. It will always point to base address. And as
pointer work from right to left so it will select i first hence it will print iye
Comments
Post a Comment