C Programming

Find Output of Program

C Programming Exercise Mode

Find Output of Program

Practice and master this topic with our carefully crafted questions.

5 Questions
8 Minutes
0% Completed
QUEST ? !
Question 1

If the size of integer is 4bytes, What will be the output of the program?

#include<stdio.h>

int main()
{
    int arr[] = {12, 13, 14, 15, 16};
    printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
    return 0;
}


A
10, 2, 4
B
20, 4, 4
C
16, 2, 2
D
20, 2, 2
Correct Answer: Option B

The array elements are 5 each of size 4 bytes so it is 20.

Where as second is size of addres not the individual element so the size of int as declared is 4 so it is 4.

Then the final one is size of individual element i.e. size of first array element so it is 4.

Question 2

What will be the output of the program ?

#include<stdio.h>
#include<string.h>

int main()
{
    int i, n;
    char *x="Alice";
    n = strlen(x);
    *x = x[n];
    for(i=0; i<=n; i++)
    {
        printf("%s ", x);
        x++;
    }
    printf("\n", x);
    return 0;
}


A
Alice
B
ecilA
C
Alice lice ice ce e
D
Alice lice ice ce e
Correct Answer: Option D

If you compile and execute this program in windows platform with Turbo C, it will give "lice ice ce e".

It may give different output in other platforms (depends upon compiler and machine). The online C compiler given in this site will give the Option C as output (it runs on Linux platform).

Question 3

What will be the output of the program ?

#include<stdio.h>

int main()
{
    int i, a[] = {2, 4, 6, 8, 10};
    change(a, 5);
    for(i=0; i<=4; i++)
        printf("%d, ", a[i]);
    return 0;
}
void change(int *b, int n)
{
    int i;
    for(i=0; i<n; i++)
        *(b+1) = *(b+i)+5;
}


A
7, 9, 11, 13, 15
B
2, 15, 6, 8, 10
C
2 4 6 8 10
D
3, 1, -1, -3, -5
Correct Answer: Option B

For (i=0; i

* (b+1) = * (b+i) +5;

N=5;

So * (b+1) is the second element. Thus in first iteration value of 2nd element i.e. 4 will become.

For i=0; 2+5=7.

i=1;then (b+1)=4+5=9.
i=2;then (b+2)=6+5=11.
i=3;then (b+3)=8+5=13.
i=4;then (b+4)=10+5=15.

So only change 2 second element hence output : 2 15 6 8 10.

Question 4

What will be the output of the program ?

#include<stdio.h>

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}


A
JCK
B
J65K
C
JAK
D
JACK
Correct Answer: Option D

Pointer always store integer value so cp will store the memory address of location where string "jack " is stored.

Step 1 : vp = &ch;
/*Will store address of ch in vp so while we print content in printf it will print asccii value of 74 i.e "J"*/

Step 2 : vp = &j;
/* It will assign address of j to vp again it will print ascii value of 65 as "A"*/

Step 3 : vp = cp;
/* In this step cp is pointing to memory locatioon where string jack is stored and we r incrementing it by two so it will point to "C" from sring "JACK" and since we hava given %S in printf so it will print content from c onwords ie "CK"*/

So final combined output will be JACK.

Question 5

What will be the output of the program ?

#include<stdio.h>
int *check(static int, static int);

int main()
{
    int *c;
    c = check(10, 20);
    printf("%d\n", c);
    return 0;
}
int *check(static int i, static int j)
{
    int *p, *q;
    p = &i;
    q = &j;
    if(i >= 45)
        return (p);
    else
        return (q);
}


A
10
B
20
C
Error: Non portable pointer conversion
D
Error: cannot use static for function parameters
Correct Answer: Option D

Static variables are declared in a function so that other functions which access it from its local function cannot change the value. Only the local function can change its value. That's why its not used in function arguments.