Tutorials  Articles  Notifications  Login  Signup

Introduction C Programming variables (Set 1) C Programming variables (Set 2) C Programming variable and function declaration (set 3) Pointers in C Pointers (set 2)



C Programming variable and function declaration (set 3)



This set contains problems that require you to read any given C program and identify it's output.


1.

What would be output of following program ?

#include<stdio.h>
int main()
{
    enum grade {A, B, C};
    enum result student1, student2, student3;
    student1 = A;
    student2 = B;
    student3 = C;
    printf("%d, %d, %d\n", student1, student2, student3);
    return 0;
}

 

A.

0, 1, 2

B.

1, 2, 3

C.

0, 2, 1

D.

1, 3, 2



2.

Take a look at the following code:

#include<stdio.h>
int main()
{
    extern int x;
    x = 34;
    printf("%d\n", sizeof(x));
    return 0;
}

Assume, this program is being executed on GCC compiler. What would be output of the program ?

A.

2

B.

4

C.

output will vary from one compiler to another compiler

D.

It will throw error. Linker Error : Undefined symbol 'x'



3.

What would be output of the following program ?

#include<stdio.h>
int main()
{
    extern int x;
    printf("%d", x);
    return 0;
}
int x=98;

 

A.

Garbage value

B.

It will throw an error

C.

0

D.

98



4.

Take a look at following code:

#include<stdio.h>
int main()
{
    char *c1;
    char **c2;
    int *c3;
    printf("%d, %d, %d", sizeof(c1), sizeof(c2), sizeof(c3));
    return 0;
}

 

Suppose this program is being run by GCC compiler on a 64-bit machine. What would be the output ?

A.

4, 4, 4

B.

2, 4, 2

C.

2, 2, 2

D.

2, 2, 4



5.

What would be output of the following code:

#include<stdio.h>
int main()
{
    struct student
    {
        char name[30];
        int age;
        float percentage;
    };
    struct student x = {"HackersFriend"};
    printf("%d, %f", x.age, x.percentage);
    return 0;
}

 

A.

Garbage values

B.

0, 0.000000

C.

It will throw runtime error

D.

None of the options



6.

What would be output of the following code:

#include<stdio.h>

int globalVariable=83;

int main()
{
    int globalVariable=80;
    printf("%d", globalVariable);
    return 0;
}

 

A.

83

B.

80

C.

It will throw error

D.

None of the options



7.

What would be the output of the following program:

#include<stdio.h>

int main()
{
    int x = 36;
    int y = 89;
    int z = 15;
    int m;
    m = x < y < z;
    printf("%d", m);
    return 0;

}

 

A.

Compile time error

B.

0

C.

1

D.

None of the options



8.

What would be output of the following program:

#include<stdio.h>

int main()
{
    extern int func(float);
    int x;
    x = func(7.82);
    printf("%d", x);
    return 0;
}

int func(int x)
{
    return (int)++x;
}

 

A.

7

B.

8

C.

7.82

D.

Compile Error



9.

What would be the output of following of code:

#include<stdio.h>
int main()
{
    int arr[5] = {9, 4};
    printf("%d, %d, %d", arr[2], arr[3], arr[4]);
    return 0;
}

 

A.

9, 4, 4

B.

Garbage values

C.

Compile Error

D.

0, 0, 0



10.

What can you say about number of iterations of for loop upon execution in following code:

#include<stdio.h>
int main()
{
    int x=9;
    for(;scanf("%s", &x); printf("\n%d", x));
    return 0;
}

 

A.

It will be executed 9 times

B.

It depends upon input

C.

It'll execute infinitely

D.

Compile Error



11.

What will be output of the following code:

#include<stdio.h>
int main()
{
    int i=9;
    {
        int i=5;
        printf("%d, ", i);
    }
    printf("%d", i);
    return 0;
}

 

A.

9, 5

B.

5, 9

C.

Compile Error: Conflicting variable name 'i'

D.

5, 5



12.

What would be output of the following code:

#include<stdio.h>
int main()
{
    union myUnion
    {
        int i;
        char ch[5];
    };
    union myUnion x;
    x.ch[0] = 7;
    x.ch[1] = 2;
    printf("%d, %d, %d", x.ch[0], x.ch[1], x.i);
    return 0;
}

 

A.

7, 2, garbage value

B.

7, 2, 0

C.

7, 2, 519

D.

None of the options



13.

What is wrong with the following code:

#include<stdio.h>
int main()
{
    print();
    return 0;
}
void print()
{
    printf("HackersFriend");
}

 

A.

No Error

B.

It will print 'HackersFriend'. But it will give warning on compilation.

C.

Compile time error: print() is called before defined.

D.

None of the options



14.

What can you say about following code:

#include<stdio.h>
int main()
{
    void x = 0;

    printf("%d", x);

    return 0;
}

 

A.

It will throw error on compilation

B.

It will print 0

C.

It will throw error on runtime

D.

None of the options



15.

What is wrong with the following code:

#include<stdio.h>
struct car
{
    char name[10];
    int price;
};
int main()
{
    car int myCarPrice;
    int x;
    printf("%d", &x);
    return 0;
}

 

A.

Syntax error: Cannot create instance of struct car with 'car int myCarPrice'

B.

No Error

C.

Error in printf(): cannot print address of variable 'x'

D.

None of the options



16.

Take a look at following line of code:

typedef enum weekdays { Sun, Mon, Tue } days;

What can you say about variable 'days' ?

A.

It is a typedef for enum weekdays.

B.

It is a variable of type enum weekdays.

C.

It will throw error

D.

None of the options



17.

What is wrong with the following code:

#include<stdio.h>

int main()
{
    int (*x)() = foo;
    (*x)();
    return 0;
}
int foo()
{
    printf("HackersFriend");
    return 0;
}

 

A.

Syntax Error on line int (*x)() = foo

B.

Compile Error: foo undeclared.

C.

No Error

D.

None of the options





HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account