• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Structure & Union

Question 1

Assume that size of an integer is 32 bit. What is the output of following program? C
#include<stdio.h>
struct st
{
    int x;
    static int y;
};

int main()
{
    printf(\"%d\", sizeof(struct st));
    return 0;
}
  • 4
  • 8
  • Compiler Error
  • Runtime Error

Question 2

C
struct node 
{ 
   int i; 
   float j; 
}; 
struct node *s[10];

The above C declaration define \'s\' to be (GATE CS 2000)

  • An array, each element of which is a pointer to a structure of type node

  • A structure of 2 fields, each field being a pointer to an array of 10 elements

  • A structure of 3 fields: an integer, a float, and an array of 10 elements

  • An array, each element of which is a structure of type node.

Question 3

Choose the correct output from the options given below:

C
#include<stdio.h> 
struct st 
{ 
    int x; 
    struct st next; 
}; 
  
int main() 
{ 
    struct st temp; 
    temp.x = 10; 
    temp.next = temp; 
    printf(\"%d\", temp.next.x); 
    return 0; 
}
  • Compiler Error

  • 10

  • Runtime Error

  • Garbage Value

Question 4

C
union test
{
    int x;
    char arr[8];
    int y;
};

int main()
{
    printf(\"%d\", sizeof(union test));
    return 0;
}
Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
  • 12
  • 16
  • 8
  • Compiler Error

Question 5

C
union test
{
    int x;
    char arr[4];
    int y;
};

int main()
{
    union test t;
    t.x = 0;
    t.arr[1] = \'G\';
    printf(\"%s\", t.arr);
    return 0;
}
Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
  • Nothing is printed
  • G
  • Garbage character followed by \'G\'
  • Garbage character followed by \'G\', followed by more garbage characters
  • Compiler Error

Question 6

C
# include <iostream>
# include <string.h>
using namespace std;

struct Test
{
  char str[20];
};

int main()
{
  struct Test st1, st2;
  strcpy(st1.str, \"GeeksQuiz\");
  st2 = st1;
  st1.str[0] = \'S\';
  cout << st2.str;
  return 0;
}
  • Segmentation Fault
  • SeeksQuiz
  • GeeksQuiz
  • Compiler Error

Question 7

Predict the output of following C program C
#include<stdio.h>
struct Point
{
  int x, y, z;
};

int main()
{
  struct Point p1 = {.y = 0, .z = 1, .x = 2};
  printf(\"%d %d %d\", p1.x, p1.y, p1.z);
  return 0;
}
  • Compiler Error
  • 2 0 1
  • 0 1 2
  • 2 1 0

Question 8

Anyone of the followings can be used to declare a node for a singly linked list. If we use the first declaration, “struct node * nodePtr;” would be used to declare pointer to a node. If we use the second declaration, “NODEPTR nodePtr;” can be used to declare pointer to a node. C
/* First declaration */
struct node {
int data;
struct node * nextPtr;
};

/* Second declaration */
typedef struct node{
int data;
NODEPTR nextPtr;
} * NODEPTR;
  • TRUE
  • FALSE

Question 9

Anyone of the following can be used to declare a node for a singly linked list and “NODEPTR nodePtr;” can be used to declare pointer to a node using any of the following C
/* First declaration */
typedef struct node
{
 int data;
 struct node *nextPtr;
}* NODEPTR;

/* Second declaration */
struct node
{
 int data;
 struct node * nextPtr;
};
typedef struct node * NODEPTR;
  • TRUE
  • FALSE

Question 10

In the following program snippet, both s1 and s2 would be variables of structure type defined as below and there won\'t be any compilation issue. C
typedef struct Student
{
 int rollno;
 int total;
} Student;

Student s1;
struct Student s2;
  • TRUE
  • FALSE

There are 22 questions to complete.

Last Updated :
Take a part in the ongoing discussion