Sunteți pe pagina 1din 16

Sources / sites

http://www.cquestions.com/2012/02/data-type-questions-in-c.html

http://www.indiabix.com/c-programming/pointers/

Directions

Step 1 : Identify solution without any assistance (compiler, text book, Google, etc).

Step 2 : Research (text book, Google), work (compiler), and implement the solution.

Step 3: Compare your expected result and actual result.

Resolve the difference by collaborating and corroborating with your class mates. If still not resolved, check with me.

Due to compilers using different standards and some not conforming to standards, it is possible for the output to be different or
incorrect. So it’s important that you identify the correct version and use that as reference for the exam preparation.

If the code generates a compilation error, then identify the reason for the error and fix it.
Questions Lesson(s) Learned
1 What is the similarity between a structure, union and
enumeration?
A. All of them let you define new values
B. All of them let you define new data types
C. All of them let you define new pointers
D. All of them let you define new structures
2 What is the outcome/output of the following C
program, if any?
#include<stdio.h>
void main()
{
union var
{
int a;
int b;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.a);
}
3 What is the outcome/output of the following C
program, if any?
#include<stdio.h>

void main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI,
SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE,
WED, THU, FRI, SAT);
}
A. -1, 0, 1, 2, 3, 4
B. -1, 2, 6, 3, 4, 5
C. -1, 0, 6, 2, 3, 4
D. -1, 0, 6, 7, 8, 9
4 What is the outcome/output of the following C
program, if any?
#include<stdio.h>
struct employee
{
char name[20];
int age;
};
void modify_byVal(struct employee v)
{
v.age=v.age+2;
}

void modify_byRef(struct employee *p)


{
p ->age=p->age+2;
}

void main()
{
struct employee Sam = {"Sam", 35};
struct employee Mary = {"Mary", 25};
modify_byVal(Sam);
modify_byRef(&Mary);

printf("%s %d", Sam.name, Sam.age);


printf(" ");
printf("%s %d", Mary.name, Mary.age);
}

5 Point out the error in the program?


#include<stdio.h>
void main()
{
union a
{
int i;
char ch[2];
};
union a z1 = {512};
union a z2 = {0, 2};
}
A. Error: invalid union declaration
B. Error: in Initializing z2
C. No error
D. None of above
6 What is the outcome of the following code
segment when compiled and executed?
#include<stdio.h>

void main()
{
struct emp
{
char gender;
int age;
char name[20];

};
struct emp e1 = {‘M’,"David", 23};
struct emp e2 = {‘M’,"David", 23};
if(e1 == e2)
printf("The structures are equal");
else
printf("The structures are not equal");
}
A. “The structures are equal”
B. “The structures are not equal” is printed
because garbage values fill up the
remaining space in variable name
C. Compile error: Structure cannot be compared
using '=='
D. No output
D. None of above

7 What is the outcome of this code when compiled


and executed?
#include<stdio.h>

void main()
{
struct emp
{
char name[25];
int age;
float bs;
};
struct emp e;
e.name = "Steven";
e.age = 25;
printf("%s %d\n", e.name, e.age);
}
8 What is the outcome of this code when compiled
and executed?
#include<stdio.h>

void main()
{
struct emp
{
char *name;
int age;
float bs;
};
struct emp e;
e.name = "Steven";
e.age = 25;
printf("%s %d\n", e.name, e.age);
}
9 Which of the following statements correctly assigns 12
to structure member month?
#include<stdio.h>

struct date
{
int day;
int month;
int year;
};
void main()
{
struct date d, *pdt;
pdt = &d;
}
I. pdt.month = 12
II. &pdt.month = 12
III. d.month = 12
IV. pdt->month = 12

A I & III
B. III & IV
C. I & III
D. II & IV
10 A union cannot be nested in a structure
A. True
B. False
11 Nested unions are allowed
A. True
B. False
12 One of the elements of a structure can be a pointer to
the same structure.
A. True
B. False
13 One of the elements of a structure can be a variable of
the same structure.
A. True
B. False
14 A structure can be nested inside another different
structure.
A. True
B. False
15 The '.' operator can be used to access structure
elements using a structure variable.
A. True
B. False
16 Union elements can be of different sizes.
A. True
B. False
17 Union elements must be different sizes.
A. True
B. False
18 The '->' operator can be used to access structures
elements using a pointer to a structure variable only
A. True
B. False
19 It is not possible to create an array of pointer to
structures.
A. True
B. False
20 Structures can be compared by using operators ==
and !=
Why?
21 Given
struct card {
char *face;
char *suit;
} c, *cPtr;

cPtr= &c;

Which of following is the proper syntax to access the


member face?

*cPtr->face
cPtr.face
cPtr->face
card->face

22 Given
union values {
char w;
float x;
double y;
};

Which of the following will work?

union values v = { 1.27 };


union values v = { ‘1.27’};
union values v = { 1 };
all the above;
none of the above
23 In an expression using the ______ operator, bits are
set to 1 if exactly one of the corresponding bits in
either operand is set to 1. Otherwise, the bits are set to
zero.

24 The bitwise AND operator (&) is often used to


_______ bits that is to select certain bits while zeroing
others.

25 In an expression using the _____ operator, bits are set


to 1 if at least one of the corresponding bits in either
operand is set to 1. Otherwise, the bits are set to zero.

26 Which bitwise operator is suitable for turning off a


particular bit in a number?
A. && operator
B. & operator
C. || operator
D. ! operator
27 Which bitwise operator is suitable for turning on a
particular bit in a number?
A. && operator
B. & operator
C. || operator
D. | operator
28 What is the outcome/output of the following C
program, if any?
#include<stdio.h>
void main()
{
int a =4,b=16;
b >> 3;
a = a << b;
printf("%d %d ", b, a );
}
29 What is the outcome/output of the following C
program, if any?
#include<stdio.h>
void main()
{
int a =4,b=2;
b=b<< 1;
a<< b;
printf("%d %d ", b, a );
}
30 Left shifting a number by 1 is always equivalent to
multiplying it by 2.
A. True
B. False
31 On left shifting, the bits from the left are rotated and
brought to the right and accommodated where there is
empty space on the right?
32 Comment on the output of this C code?

int main()
{
int i, n, a = 4;
scanf("%d", &n);
for (i = 0; i < n; i++)
a = a * 2;
}
• A. Logical Shift left
• B. No output
• C. Arithmetic Shift right
• D. bitwise exclusive OR
33
What is the outcome of this code when compiled and
executed?
#include<stdio.h>
void main()
{
    int a=4;
    int b=8;
    int c= a |  b;
    printf("%d\n",c);
  
}

34
What is the outcome of this code when compiled and
executed?
#include<stdio.h>
void main()
{
    int a=4;
    int b=8;
     if ( a & b)
         printf("True");
   else
        printf("False");
}

35 Intentionally left blank

S-ar putea să vă placă și