Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 122

General Set:

Ans: C
Ans: D

For(/*initiulization*/;/*condition*/;/*operation*/)

Initlization – first time, only once before the block

Condition –before every iteration

Operation-after every iteration

i=i+1

i+= 1

i+=i+1

above means i= i+(i+1);


Ans: D
Ans: A

5
.

Ans: A (x is implicitly converted to float) – Type promotion

6
Ans: B

Following is the declaration for printf() function.


int printf(const char *format, ...)

Printf returns the number of printed characters


7.

Ans: c
8.

Ans: B (declaration statement syntactically wrong)


9.

Ans: B
10.

Ans: c ( 3[x] is allowed means 3+x which is x+3)

x[3] same as *(x+3)

3[x] same as *(3+x)

We can’t change base address of an array

We can’t change value by using pointer - A

We can change value using pointer

#include <stdio.h>

int main(void) {

int a=25;
int *ptr;

ptr=&a;

*ptr=50;

printf("%d", a);

return 0;

#include <stdio.h>

int main(void) {

int arr[]={11,21};

int *ptr;

ptr = arr;

*ptr = *ptr+1;

ptr++;

/*arr++; compiler error */

*ptr = *ptr+1;

printf("%d %d", arr[0], arr[1]);

return 0;

}
11.

Ans: c

Octal ints prefixed with 0, Hexadecimal prefixed with 0x

%x prints in hexadeimcal format

%o ?
12.

Ans: B
13.

Ans: A

Printf() function returns number of characters printed


14.

Ans: B

Doesn’t get into for loop, as condition is false

For(;;) – infinite loop, u can come out of this using break


For(;;); - infinite loop

For(x=0,y=10;x<=10&&y>=9;x++,y--)

15.
Ans: D

Sizeof(a) – 11+1(null character)

Sizeof(p) – 2 size of address, p is just a pointer

Another Question:

Int arr[] = {11,23,14};

Int *parr;

Parr = arr;

Print sizeof(arr) sizeof(parr)


16.

Ans: A

\b means last printed character is removed in display


17.

Ans: C
18.

Ans: A

01010

10100

So 10&20 is 0

10/20 is 0 (since both oepranfs are int, result is int)


19.

Ans: D
20.

Ans: A

X is printed, and y not printed since specifier not there


21.

Ans: D

Union takes maximum size of the member


22.

Ans: B
? : conditional or ternary operator

Evaluate c>1 first

Evaluate from left

23.
Ans: A
24.

Ans: A

Since compiler will not take 0.1 exactly as 0.1, and will be slightly different
25.

Ans: B

(printf does not belong to for loop, reason is for loop ends where ; is used with fro)

At some point of time conditiond becomes 0


26.

ANs: D
27.

Ans: B
28.

Ans: A(Since static are initialized with 0 by default)


29.

Ans: B

I local value is considered, which is not initialized, hence it is garbage


30.

Ans:B

Decimal to someother – division-get reminders

Someother to decimal - multiply


31.

Ans: c
32.

Ans: c

 And / has high precedence, both have same precedence, associativitivity is left to right
33.

Ans: D (compiler err)


34.

Ans: D
35.

Ans: B
36.

Ans: C (type casting is done)


37.

Ans: C
38.

Ans: B
39.

Ans: C
40.

Ans: A(assuming size of int is 2 bytes)

A will not be incremented in sizeof


41.

Ans: C

Printf() return number of printed characters


42.

Ans: B
43.

Ans: A

 And % has same precedence, associativity left to right


44.

Ans: c

Though you can’t store function in struct and union, function pointer can be a member of struct or
union
45.

ANs: D
46.

Ans:A (continue can be used in loops only)


47.

ANs: C
48.

Ans: A

a[10] size is 10*2 = 20

char p = 1

Total s ize is 21
49.

Ans:dbye
50.

ANs: c
51.

Ans:

1 60

2 80

3 40
4 70

…..

Storage classes – there r four storage classes

52.
Ans:

3 60

4 80

5 40

….
53.

6 60

6 80

6 40


54.

Ans: 3 60

3 80

3 40
55.

ANs: D
56.

Ans:

11759
.3 .3 .4 .2 .6

Here p and q pointers itself are not udpated

Arithmetic operations on pointers, if +1, will refer to next element

57.

4
8

58.
Ans: D

%o – print in octal form

%x-print in hexadecimal
59.

Ans: C
60.

Ans: D

Sizeof(*x) – one byte

Sizeof(x) – null character is inckuded in size


61.

Ans: C

A=!(1)&&++b;

A=0

C = c+(a+b--)

C= 1+(0+2) = 3

B =1
62.

Ans: D
63.

Ans: B

++ has associativity left to right


64.

Ans: B
65.

Ans: A
66.

Ans: D

Static storage class


67.

Ans: D
68.

Ans:c
69.

Ans: D

# Is stringize operator

Using stringize operator we can convert some text into string without using any quotes

Also, explore ## - tokenizing operator


70.

ANs: B
71.

Ans: D

A=1,3,15; a takes first value

B=(2,4,6); b takes last value


72.

Ans: c
73.

Ans:B
74.

Ans: d

Sizeof considers null char

Strlen does notconsider null


75.

Ans:c
76.

Ans:

P=2

10 – size 3 bits = 2

C=-6
1010 – size 3 bits = 2

M=5

101 – size 2 bits = 1

77.
Ans:b

78.
Ans:b

Double takes 10 bytes

Long int 8 bytes


79.

Ans: C
80.

Ans: D

0,1,2,3,4
81.

Ans: C
82.

Ans: B
83.

Ans: D

Scanf() returns number of values read


84.

Ans: A
85.

Ans: B
86.

Ans: A

&& has high precedence compared to ||

Z++ gets executed


87.

Ans: 8
88.

Ans: 0

J = i+10 doesn’t get computed as I is zero


89.

Ans:
90.

Ans: B
91.

Ans: C

A is aasigned to 5
92.

Ans: A
93.

Ans: A

X gets incremented to 1, due to x++


94.

Ans: A
95.

Ans: D

Static cannot be assigned to value, it has tobe literal value


96.

Ans: D

Ascii value of a is 97
97.

Ans: C
98.

Ans:
99.

Ans:C

(b+1)[5] is b[6]
100.

Ans: B

First character gets \0 is first character

Pointers Set:
101.
1. #include <stdio.h>
2.  
3. int main(void) {
4. int i = 0;
5. ++i++;
6. printf("%d",i);
7. return 0;
8. }

Ans: Compiler error

102.

#include <stdio.h>

int *fun();

int main(void) {

int *myptr;

myptr = fun();

printf("%d", *myptr);

return 0;

int *fun()

int num = 25;

int *ptr = &num;

return ptr;

103.

void swap(int *p, int *q)


{

Int tmp;

tmp = *p;

*p = *q;

*q = tmp;

Make necessary changes and call above function to swap two numbers

Can it be changed to support to swap any data type variables.

104.

Callback does not work without

a) Increment operator
b) -> operator
c) Function pointer
d) Linked list

Ans: c

105. initialize two dimensional array,

a) find max. value in each row


b) find max value in each column
c) find sum of elements of both the diagonals

106. Which of below operations can be performed on pointers

Addition, subtraction, multiplication, division

107. pointer to a pointer

108. How to dynamically allocate memory

#include <stdio.h>

int *fun();

int main(void) {
int *ptr = (int *)malloc(sizeof(int));
*ptr = 25;
printf("%d", *ptr);
free(ptr);

return 0;
}

109. Pointer to function

int myfun()

printf(“This is used to test function pointer\n”);

return 0;

Int main()

Int (*fptr)();

fptr = myfun;

fptr();

return 0;

Also, explore

#200.

1. #include <stdio.h>
2.  
3. int main(void) {
4.  
5. int i=0,j=10;
6. /*
7. inititlization, operation
8. */
9. for(i=0,j=10;i<=10&&j>=0;i++,j--)
10. {
11. printf("%d,%d\n",i,j);
12. }
13.  
14. return 0;
15. }

#201.

#include <stdio.h>

int main(void) {

int i=0,j=10;

/*

inititlization, operation

*/

for(i=1;i<=5;i++)

for(j=1;j<=10;j++)

printf("%d X %d = %d\n",i,j,i*j);

printf("\n\n");

return 0;

#202.

#include <stdio.h>

int main(void) {
int arr[] = {11,22,23,21,63,52,12};

int *ptr;

ptr = arr;

printf("%d\n", *ptr);

ptr = ptr + 1;

printf("%d", *ptr);

return 0;

#205.

#include <stdio.h>

struct employee{

int empid;

char *name;

float salary;

char *addr[4];

};

int main(void) {

struct employee emp[10];

/*printf("size of emp: %d", sizeof(emp1));*/


emp[0].empid = 1;

emp[0].name = "kumarEg";

emp[0].salary = 123.4;

emp[0].addr[0] = "Banaglore";

printf("%s %s", emp[0].name, emp[0].addr[0]);

return 0;

#206.

#include <stdio.h>

struct employee{

int empid;

char *name;

float salary;

char *addr[4];

};

void fun(struct employee pemp) /*formal argument */

printf("in function %s\n", pemp.name);

int main(void) {
struct employee emp;

struct employee *sptr;

sptr = &emp;

/*printf("size of emp: %d", sizeof(emp1));*/

(*sptr).empid = 1;

sptr->name = "kumarEg";

sptr->salary = 123.4;

sptr->addr[0] = "Banaglore";

printf("%s %s", sptr->name, sptr->addr[0]);

fun(emp); /* actual parameter */

return 0;

#207.

for(i=0;i<len-1;i++);

printf("check:%c\n", sptr->name[i]);

#208. Diff b/n array of pointers and pointer to an array


#209. Pointer to pointer
int num = 10;
int *ptr;
ptr = &num;

printf("%x %d\n", ptr, *ptr);

int **pptr; /* pointer to a pointer */


pptr = &ptr;

printf("%d", **pptr);

#210.
#include <stdio.h>

int main(void) {
int arr[] = {11,21,2,65,23,31};

printf("%d", *arr);
return 0;
}

#211.
/* CALL BY VALUE */
int main(void) {
int arr[] = {11,21,2,65,23,31};
int num = 25;
/*printf("%d", *arr); */
fun(num);
printf("in main() %d", num);
return 0;
}

fun(int i)
{
printf("(in fun() number is %d\n", i);
i++;
return;
}

#212.
#include <stdio.h>

/* CALL BY Reference */
int main(void) {
int arr[] = {11,21,2,65,23,31};
int num = 25;

/*printf("%d", *arr); */
fun(&num);
printf("in main() %d", num);
return 0;
}

fun(int *pi)
{
printf("(in fun() number is %d\n", *pi);
(*pi)++;
return;
}

#213.
#include <stdio.h>

/* Array always sent by, CALL BY Reference */


int main(void) {
int arr[] = {11,21,2,65,23,31};

/*printf("%d", *arr); */
fun(arr);
printf("in main() %d", arr[0]);
return 0;
}

fun(int arrp[])
{
arrp[0] = 11111;
return;
}

#215.

int arr[10];
a)Array has fixed number of elements
b)difficult to remove/add elements in between in run time
c)Accessing of any element is very fast by using index
d) all element stored sequentially in memory

LinkedList:
Number of elements can be added/removed dynamically
Accessing of elements is slower
elements are scattered across memory

#300. Pointer to function


#include <stdio.h>

void func()
{
printf("This is a function");
}

int main(void) {
/*void (*pfunc)(int, int);*/
void (*pfunc)();
pfunc = func;

pfunc();

return 0;
}

#301 Void * - generic pointer


#include <stdio.h>

void func()
{
printf("This is a function");
}

int main(void) {
void *vptr;
int i=35;
float f=23.1;

vptr = &i;
printf("%d\n", *(int *)vptr);

vptr = &f;
printf("%f", *(float *)vptr);

exit(0);
void (*pfunc)();
pfunc = func;

pfunc();

return 0;
}

#302.
Void * casted to ptr to function
#include <stdio.h>

void func()
{
printf("This is a function");
}

int main(void) {
void *vptr;
int i=35;
float f=23.1;

vptr = &i;
printf("%d\n", *(int *)vptr);

vptr = &f;
printf("%f", *(float *)vptr);
vptr = func;

((void (*)())vptr)();

return 0;
}

#303. Union example


#include <stdio.h>

union abc{
int i; /* 4 */
float f; /* 4 */
char ca[20]; /* 20 */
};

int main(void) {
union abc uabc;
printf("%d\n", sizeof(union abc));

uabc.i = 25;
printf("%d\n", uabc.i);

uabc.f = 34.21;
printf("%d", uabc.i);

return 0;
}

#304. Use pointers carefully


int i=30;
int *pi;
pi = &i;

pi++;

printf(*pi);

#310. malloc
#include <stdio.h>

int main(void) {

int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 28;

printf("%d", *ptr);

free(ptr);
return 0;
}

#311. Calloc eg
int main(void) {

int *ptr;
ptr = (int *)calloc(sizeof(int)/* size of one block*/, 1/* number of blocks*/);
*ptr = 28;

printf("%d", *ptr);

free(ptr);
return 0;
}

#312. Realloc

#313.Steps in execution of .c file


Preprocessor – removes #define, #include, etc…
Compiler – converts pro[rocessed code to object(.o file)
Linker- combine individual objects (.o files) to create exe file
Loader-loads executable file into RAM, and start execution

#314.
#include – to include header file
#define – to define macro
#if – conditional compilation
#elif – conditional compilation
#else - conditional
#endif
#ifdef
#ifndef
#undef
#pragma – instructing compiler
#error – raise error during preprocessing

# - strignignzing operator
## - tokenizer

    #ifdef x
      printf("hello\n");      // this is compiled as  x is
defined
   #else
      printf("bye\n");       // this isn't compiled
   #endif

    #if x==10
      printf("hello\n");      // this is compiled as  x is
#elif x==12
Printf(“I don’t care”);   
#else
      printf("bye\n");       // this isn't compiled
   #endif

#if x==-1
#error “x value is invalid
#endif

#400. Constant pointer


#include <stdio.h>

int main(void) {
int i = 10, j= 20;

int * const ptr = &i;

printf("%d\n", *ptr);

/*
Error as ptr is declared as constant
ptr = &j;

printf("%d\n", *ptr);*/
return 0;
}

#401. Pointer to a const


#include <stdio.h>
int main(void) {
int i = 10, j= 20;

/* pointer to a constant integer */


const int * ptr = &i;

/*int const * ptr = &i; */


printf("%d\n", *ptr);

ptr = &j;

printf("%d\n", *ptr);
return 0;
}

#402.
/* constant pointer to a constant integer */
const int * const ptr = &i;

You might also like