0% found this document useful (0 votes)
6 views24 pages

C Programming

The document contains multiple-choice questions (MCQs) related to C programming, covering topics such as function pointers, memory management, data types, and program outputs. Each question presents several options, requiring the reader to select the correct answer based on their knowledge of C programming concepts. The questions range from basic syntax to more complex programming scenarios.

Uploaded by

Mamatha bg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

C Programming

The document contains multiple-choice questions (MCQs) related to C programming, covering topics such as function pointers, memory management, data types, and program outputs. Each question presents several options, requiring the reader to select the correct answer based on their knowledge of C programming concepts. The questions range from basic syntax to more complex programming scenarios.

Uploaded by

Mamatha bg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming - NXP

MCQs
1.​ Among the below options, which is the correct way to pass a function pointer
to arguments
A. void transport(int (*fptr)(int, float, char)){}
B. void transport(*fptr(int, float, char)){}
C. void transport(int (*fptr)){}
D. void transport(*fptr){}

2.​ Print first x characters of a string using printf();


char a[] = "Hello World";
int x = 5;
A. printf("%d %s\n", x,a);
B. Printf("%.*s\n", x, a);
C. printf("%. %ds\n",x,a);
D. Not possible

3.​ Update memory location OxABCD with value Ox12345678:


A. (int*)0xABCD = Ox12345678;
B. *(int *)&0xABCD = Ox12345678;
C. *(int *)0xABCD - Ox12345678;
D. None of the above

4.​ 32-bit memory with base address 0xA000 which is word accessible for write
and byte accessible for read. Which of the following are valid?
(i) Can write value 0x12345678 to address 0xA003
(ii) Can write value Ox12345678 to address 0xA004
(iii) Can write value Ox12345678 to address 0xA002
(iv) Can read 32-bit word from address 0xA008
(v) Can read 32-bit word from address OxA007
(vi) Can read a byte from address OxA003
A. i, ii, iii, iv and v
B. ii and vi
C. i, iii and v
D. All

5.​ Consider the following program


void main(void)
{
union
{
struct
{
char myChar1[2];
char myChar2[2];
}s1;
struct
{
int i;
int j;
}s2;
}u = {12,1};
printf("%d %d", u.s2.i, u.s2.j);
}

Output of this program is:


A. 12 0
B. 12 1
C. 268 0
D. 0 268

6.​ Consider the following statements


X=> Different enumeration may have same name
Y=> Values must be distinct in the same enumeration
A. both statements above are true
B. both statements above are false
C. X is true but Y is false
D. X is false but Y is true

7.​ Consider the following program


static int a;
char b[10];
struct st
{
float x,y;
}pt;

void main(void)
{
static float v[] = {1.2, 1.3};
static struct st pt1;
printf("%f %f, v[1], v[2]");
}
Which statement is true?
A. Except v, all variables reside in bss
B. All variables reside in bss
C. only v reside in bss
D. No variables reside in bss

8.​ Consider a static variable is defined in header file, which is being used in
multiple C files, and the value of the variable is assigned in one of those c files.
Which statement is true?
A. Program will not compile
B. Program will compile and value of variable will be same in all files which is
assigned in one of the file
C. Program will compile and value of variable will be different and local to each
c file
D. None of above is true

9.​ calloc (m,n) is equivalent to


A. p = malloc (m*n); memset(p,1,n);
B. p = malloc (m*n); memset(p,0,m*n);
C. p = malloc (m*n); memset(p,0,m);
D. p = malloc (m); memset(p,0,m*n);

10.​int myArr[5][6][7]
Type of myArr[0] is:
A. Pointer to Array [5] of int
B. Array [6] of Array [7] of int
C. Pointer to Array [6] of Array [7] of int
D. Array [7] of Array [6] of int

11.​Consider the following code


char * ptr[3]= {“First”,”Second”,"Third"};
which statement will print “Second”
A. printf("%s", *ptr[1]);
B. printf("%s", *ptr[0]);
C. printf("%s",*(ptr+1));
D. printf("%s", ptr[0]);

12.​#include <stdio.h>
void main()
{
m();
m();
}
void m()
{
static int x = 6;
x++;
printf("%d", x);
}
A. 7 8
B. 6 6
C. 7 7
D. 6 7

13.​Which of the following is true about arrays in C


a.​ For every type T, there can be an array T
b.​ For every type T except void and function type, there can be an array T
c.​ When 2D array is passed to the function, C compiler creates copy of the
array
d.​ 2D arrays are stored in column major form
14.​ What is the output of the following C program
int main()
{
​ int _int = 5;
​ printf(“%d” , _int);
}
a.​ Runtime Error
b.​ 5
c.​ Garbage value
d.​ Compiler Error

15.​Predict output of the program


int main()
{
​ unsigned int i = 65000;
​ while (i++ != 0);
​ printf(“%d” , i);
}
a. Runtime Error
b. 0
c. 1
d. Infinite loop

16.​What is the output of this program?


using namespace std;
int main()
{
​ char i = 0;
​ for (; i++; printf(“%d”, i))
​ ;
​ printf(“%d”, i);
​ return 0;
}
a.​ 0
b.​ 1
c.​ Infinite loop
d.​ Compiler error
17.​ What is the output of this program? Consider ‘b’ as user input.
#include <stdio.h>
int main()
{
​ int i = 1, j = 1;
​ for (--i && j++; i < 10; i += 2)
​ ​ printf(“loop ”);
​ return 0;
}
a.​ Compiler error
b.​ Program never ends
c.​ loop loop loop loop loop
d.​ None of the above

18.​What is the output of this program?


#include <stdio.h>
int main()
{
​ unsigned char i = 0;
​ for (; i<= 0; i++);
​ printf(“%d”, i);
​ return 0;
}
a.​ 127
b.​ 128
c.​ Program never ends
d.​ 0

19.​What is the output of this program?


#include <stdio.h>
#define start main
void start()
{
​ printf(“Hello ”);
}
a.​ No print
b.​ Hello
c.​ “”
d.​ None
20.​What is the output of this program?Assume sizeof int is 2 bytes
#include <stdio.h>
int main()
{
​ struct employee
​ {
​ ​ int emp_id[5];
​ ​ int salary;
​ ​ employee *s;
​ }emp;
​ printf(“%d %d\n”, sizeof(employee), sizeof(emp.emp_id));
}
a.​ 6 2
b.​ 8 2
c.​ 14 10
d.​ 12 10

21.​What is the output of this program?


#include <stdio.h>
unsigned long int (*function())[5]
{
​ static unsigned long int arr[5] = {2, 3, 5, 7, 11};
​ printf(“%d ”, *arr);
​ return &arr;
}
int main()
{
​ unsigned long int (*ptr)[5];
​ ptr = function();
​ printf(“%d “, *(*ptr + 4));
}
a.​ 2 5
b.​ 2 7
c.​ 2 11
d.​ Compiler error

22.​What is the output of this program?


#include <stdio.h>
int main()
{
​ void *ptr;
​ int num = 10;
​ ptr = &num;
printf(“%d “, ptr);
}
a.​ 10
b.​ Compiler Error
c.​ 0
d.​ Address

23.​What is the output of this program?


int foo(const int *p)
{
*p = 10;
}

int main()
{
const int i = 5;
foo(&i);
}

24.​What is the output of this program?


int main()
{
printf("%d\n", sizeof(main));
}

25.​What is the output of this program?


int main()
{
​ int i = 0x00737542;
​ printf("%s\n", (char*)&i);
}
26.​What is the output of this program?
int main()
{
​ int i;
​ for (i = 0; i < 3; i++)
​ {
​ ​ int k = 10;
​ ​ printf("%d\n", k);
​ ​ k = 40;
}
}
a.​ Compile time Error
b.​ 10 40 40
c.​ 10 10 10
d.​ None of the above

27.​What is the output of this program?


#include <stdio.h>
union u
{
​ int a;
​ short b[2];
};
int main()
{
​ union u temp = {10};
​ temp.b[1] = 37;
​ printf("%d\n", temp.a);
}
a.​ 37
b.​ 2424482
c.​ 2424842
d.​ None of the above

28.​What is the output of this program?


int main()
{
​ int i;
​ for (i = 0; ++i < 10; i++)
​ ​ printf("%d\n", i);
​ return 0;
}
a.​ 1 2 3 4 5 6 7 8 9 10
b.​ 12468
c.​ 13579
d.​ None of the above

29.​What is the output of this program?


#include <stdio.h>
void print(const int *i)
{
​ *i = 20;
}
int main()
{
​ const int x = 10;
​ printf("%d\n", x);
​ print(&x);
​ printf("%d\n", x);
}
a.​ Compile time Error
b.​ 10 20
c.​ 10 10
d.​ Undefined value

30.​What is the output of this program?


#include <stdio.h>
#define SYSTEM 10
int main()
{
​ int num = 10;
​ #if SYSTEM == num
​ printf(“Welcome to “);
​ #endif
​ #if SYSTEM == 10
​ printf(“NXP“);
​ #endif
}
a.​ Welcome to
b.​ Welcome to NXP
c.​ NXP
d.​ No output
31.​What is the output of this program?
#include <stdio.h>
int num = 15;
void main()
{
​ int num = 14;
​ m();
​ printf(“%d”, num);
}
int m()
{
​ num = 12;
​ n();
}
int n()
{
​ printf(“%d”, num);
}
a.​ 12 15
b.​ 12 14
c.​ 14 15
d.​ 15 12

32.​What is the output of this program?


#include <stdio.h>
struct ptr
{
​ int a;
​ int b;
};
int main()
{
​ struct ptr temp[] = {1, 2, 3, 4, 5, 6};
​ struct ptr *ptr1 = temp;
​ printf(“%d %d\n”, ptr1 -> a, (ptr1+ 2) -> b);
}
a.​ 1 5
b.​ 1 4
c.​ 1 2
d.​ 1 6
33.​What is the output of this program?
#include <stdio.h>
int main()
{
​ int three = 3, four = 4;
​ #ifdef next
​ three = 4;
​ four = 3;
​ #endif
​ printf(“%d %d “, three, four);
}
a.​ Compile time Error
b.​ Run time Error
c.​ 3 4
d.​ 4 3
34.​What is the output of this program?
#include <stdio.h>
void f(int (*x)(int));
int myfoo(int);
int (*fooptr)(int);
int ((*foo(int)))(int);
int main()
{
​ fooptr = foo(10);
​ fooptr(11);
}
int ((*foo(int)))(int)
{
​ return myfoo;
}
int myfoo(int i)
{
​ printf(“%d\n”, i + 1);
​ return 0;
}
a.​ 11
b.​ 12
c.​ Compile time Error
d.​ Undefined behaviour

35.​What is the output of this program?


#include <stdio.h>
void f1(int arr[5])
{
​ printf(“%d\n”, sizeof(arr));
}
void f()
{
​ int arr[6] = {1, 2, 3, 4, 5, 6};
​ printf(“%d\n”, sizeof(arr));
​ f1(arr);
}
void main()
{
​ f();
}
a.​ 24 24
b.​ 24 4
c.​ 6 24
d.​ 6 6
36.​ What is the output of this program?
#include <stdio.h>
void main()
{
​ if (!0 == 1)
​ ​ printf(“yes”);
​ else
​ ​ printf(“no”);
​ if (~0 == 1)
​ ​ printf(“yes”);
​ else
​ ​ printf(“no”);

}
a.​ yes yes
b.​ yes no
c.​ no yes
d.​ no no

37.​Which of the following isinvalid assignment operator


a.​ a %= 10;
b.​ a |= 10;
c.​ a /= 10;
d.​ None of the above

38.​What is the output of this program?


#include <stdio.h>
void main()
{
​ int m = -14, n = 6, o;
​ o = m % ++n;
​ n += m++ - o;
​ m <<= (o ^ n) & 3;
​ printf(“m = %d n = %d”, m, n);
}
a.​ m = -20 n = 7
b.​ m = -27 n = -6
c.​ m = -26 n = -7
d.​ None of the above
39.​What is the output of this program?
#include <stdio.h>
void main()
{
​ printf(“%d\n”, sizeof(main));
}
a.​ 0
b.​ 1
c.​ Undefined behaviour
d.​ Linker Error

40.​ What is the output of this program?


#include <stdio.h>
void main()
{
​ struct site
​ {
​ ​ char name[] = “Hello world”;
​ ​ int no_of_pages = 200;
​ };
​ struct site *ptr;
​ printf(“%d”, ptr -> no_of_pages);
​ printf(“%s”, ptr -> name);​
}
a.​ 200 Hello world
b.​ 200
c.​ Compiler error
d.​ Runtime Error

41.​Assume sizeof integer and pointer is 4 byte. What will be the output?
#include <stdio.h>
#define R 10
#define C 20
void main()
{
​ int (*p)[R][C];
​ printf(“%d \n”, sizeof(*p));
}
a.​ 200
b.​ 4
c.​ 800
d.​ 80

42.​ What is the output of this program?


#include <stdio.h>
void main()
{
​ unsigned int m = 32;
​ printf(“%d %d”, m >> 1, ~m);
}
a.​ 16 5
b.​ 64 -32
c.​ 16 -33
d.​ 16 33

43.​What is the output of this program?


#include <stdio.h>
void main()
{
​ int a = 5
​ float b;
​ printf(“%d”, sizeof(++a + b));
​ printf(“%d”, a);
}
a.​ 2 6
b.​ 4 6
c.​ 2 5
d.​ 4 5

44.​What is the output of this program?


#include <stdio.h>
void main()
{
​ void *vptr, v;
​ v = 0;
​ vptr = &v;
​ printf(“%v”, *vptr);
}
a.​ 0
b.​ Compiler error
c.​ Runtime Error
d.​ Garbage value

45.​In C, static storage class can’t be used with


a.​ Global variable
b.​ Function parameter
c.​ Function name
d.​ Local variable

46.​What does the following fragment of C program print?


char c[] = “GATE2011”;
char *p = c;
printf(“%s”, p + p[3] - p[1]);
a.​ GATE2011
b.​ E2011
c.​ 2011
d.​ 011

47.​ If a static global variable is declared in a header file, and the header file if
included in multiple source file. What happens to the scope of the variable.
a.​ Compilation Error
b.​ Duplicate copies get created for files in which it’s included
c.​ There will be only one variable shared with all files.
d.​ Runtime Error

48.​What is the output of this program?


#include <stdio.h>
#define f(g, g2) g##g2
void main()
{
​ int var12 = 100;
​ printf(“%d \n”, f(var, 12));
}
a.​ 100
b.​ 0
c.​ 1
d.​ Compiler Error

49.​Suppose someone writes increment macro(increments value by 1) in following


ways
#define INC1(a) ((a) + 1)
#define INC2 (a) ((a) + 1)
#define INC3( a ) (( a ) + 1)
#define INC4 ( a ) (( a ) + 1)
Pick the correct statement for above macros
a.​ Only INC1 is correct
b.​ All 4 macros are correct
c.​ Only INC1 and INC2 is correct
d.​ Only INC1 and INC3 is correct

50.​Which of the following is true about return type of functions in C?


a.​ Function can return any type
b.​ Function can return any type except arrays and functions
c.​ Function can return any type except arrays and functions and union
d.​ Function can return any type except arrays and functions, function pointer
and unions

51.​What is the output of this program?


#include <stdio.h>
int main()
{
​ void demo();
​ void (*fun)();
​ fun = demo;
​ (*fun)();
​ fun();
​ return 0;
}
void demo()
{
​ printf(“Hello World”);
}
a.​ Hello World
b.​ Hello World Hello World
c.​ Compiler Error
d.​ Blank screen

52.​What is the output of this program?


#include <stdio.h>
int main()
{
​ void *ptr = NULL;
​ unsigned char counter = 30;
​ ptr = &counter;
​ ptr++;
​ printf(“ptr = %x”, *ptr);
}
a.​ Compiler Error
b.​ Garbage value
c.​ 0x100
d.​ 30

53.​What’s the meaning of using extern before function declaration?


For example, following fun sum is made extern
extern int sum(int x, int y, int z)
{
​ return x + y+z;
}
a.​ Function is made globally available
b.​ extern means nothing, sum() is same without extern keyword
c.​ Function need not to be declared before it’s use
d.​ Function is made local to the file.

54.​Predict output of following program


int main()
{
​ int i;
​ int arr[5] = {1};
​ for (i = 0; i < 5; i++)
​ ​ printf(“%d”, arr[i]);
​ return 0;
}
a.​ 1 followed by 4 garbage values
b.​ 10000
c.​ 11111
d.​ 00000

55.​ What is the output of this program?


#include <stdio.h>
int main()
{
​ int x = 5;
​ printf(“%d”, x << 1);
​ return 0;
}
a.​ 10
b.​ 15
c.​ 5
d.​ 20

56.​ What is the output of this program?


#include <stdio.h>
int main()
{
​ int x = 3, y = 5;
​ printf(“%d”, x + (~y) + 1);
​ return 0;
}
a.​ 2
b.​ 3
c.​ -2
d.​ 1
57.​What is the output of this program?
#include <stdio.h>
int main()
{
​ int x = 10, y = 10;
​ if ( !(x ^ y))
​ ​ printf(“x is equal to y”);
​ else
​ ​ printf(“x is not equal to y”);
​ return 0;
}
a.​ x is equal to y
b.​ x is not equal to y
c.​ 0
d.​ NULL

58.​What is the output of this program?


#include <stdio.h>
int main()
{
​ unsigned char i = 0;
​ for (; i <= 0; i++);
​ printf(“%d”, i);
​ return 0;
}
a.​ 127
b.​ 128
c.​ Program never ends
d.​ 0

Subjective Questions
1.​ Difference between struct and union
2.​ What do you mean by nested structure? Explain with an example.
3.​ Difference between C and C++ programming.
4.​ Can we call main inside main? Justify your answer.
5.​ How to detect memory leak in the code(at which line), about key in database
6.​ What’s the difference between # Include <> and "" and can we include other than
.h file and if yes how if no why
7.​ In an Inverted Search project, How many structures are used. Write down the
structure.

8.​ Explain Volatile keyword

9.​ What are storage classes?Draw the memory layout.

10.​What are storage classes? Explain with lifetime and scope.


11.​Const and volatile keyword with example
12.​What are Bitfields? What are the advantages of using it?
13.​Explain stages of compilation in detail.

14.​Why do you need intermediate code?

15.​Explain memory layout

16.​Explain different storage classes with examples.

17.​Difference between static and extern keyword

18.​Difference between typedef and #define

19.​Can we write 2 defaults in switch case

20.​How many times initialization happens in for loop.

21.​What is structure padding? Give an example.

22.​How int x = 10; will store in memory.

23.​extern const int a;​


Is there any error in the above declaration?

24.​Explain register variable.

Programming Questions
1.​ WAP to check if the entered string is palindrome or not.
2.​ WAP to check if the entered number is palindrome or not.
3.​ Program to reverse a string
4.​ A string is given, display the count of the characters which repeated consecutively.
5.​ Write a program to swap a nibble in a byte
6.​ C-programming if a number ex.7 has binary representation of all 111 so u should
print a msg “all one’s”.if number had 0 in between ex.5 binary is 101..so should
print a msg “all are not one’s”

i/p 11111111 o/p “All are ones”


i/p 11011111 o/p “All are not ones”
7.​ Write a program to reverse a string ,the position of special char shouldn’t
change
i/p ##!abc$
o/p ##!cba$
8.​ WAP to find the transpose of a given matrix.
9.​ Simple basic programs to perform bitwise operations(set, clear, get, toggle a
bit)
10.​In a given number, check if the 7th bit is set. If it’s true, clear the 12th bit in a
given number
11.​
12.​WAP to check entered number is 2 power of n or not
13.​WAP to check entered number is even or odd using bitwise operator
14.​WAP to print all odd elements in a given array using bitwise operators.
15.​WAP to print sizeof different data types.
16.​WAP to explain static storage class
17.​WAP to explain extern storage class
18.​Read a line, count all special char and store each special char in another file and
print count of special char
19.​A random string is given store it in nxn matrix form and padding should be done if
the characters are less than the size nxn
20.​How can we check if a bit is set or not? Write only the function.
21.​Program to perform encryption and decryption of a given string.
22.​Write a c code to print 1 to 100 prime numbers. And List out the testing cases for
that code.

Project
1.​ (Project name : password based lock system)
Requirements:
1. Change the password when user forgot it.
2. Every 10 days the password will get expired. System has to inform about the
password expiry and ask the user to change it.
3. The condition is the system should not accept the last 3 password, when the user
enter.
2.​ Explain LSB steganography project.
3.​ problem definition
Tag length
80 8
81 16
82 32
83 64
84 128
From IO we are getting some random data, you have to read that data byte by byte
and search for the tag and length here length defines the data size of the tag.
Suppose if we get 81 then 16,then we have to take next 16 bytes as data. Once you
got the tag, length, data store it in a memory. Means we have to create database
and store tag , length and data there.
They wanted to know how do you approach the problem definition, how do you
write the algorithm and code.
Which data structure you will choose.
Note : This problem is similar to mp3 tag reader project
4.​

You might also like