C Programming Test And Answer 08

1.What will be the output of the program ?

#include<stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d\n", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}

A. 5
B. 25
C. 125
D. Garbage value

Explanation:
aa = &a; // *aa = 5;

**ptr = &aa; ptr is a pointer to a pointer aa.

Hence value stored in a can be accessed using **ptr(which is **ptr = 5).

So the b = **ptr **ptr means b = 55 which is 25.

2.What will be the output of the program?

#include<stdio.h>
int main()
{
    int i=3;
    switch(i)
    {
        case 1:
            printf("Hello\n");
        case 2:
            printf("Hi\n");
        case 3:
            continue;
        default:
            printf("Bye\n");
    }
    return 0;
}

A. Error: Misplaced continue
B. Bye
C. No output
D. Hello Hi

Explanation:

The keyword continue cannot be used in switch case. It must be used in for or while or do while loop. If there is any looping statement in switch case then we can use continue.

3.What will be the output of the program?

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

Explanation:
Step 1: Initially the value of variable i is ‘5’.
Loop 1: while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while condition is satisfied and it prints ‘4’. (variable ‘i’ is decremented by ‘1’(one) in previous while condition)
Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while condition is satisfied and it prints ‘3’. (variable ‘i’ is decremented by ‘1’(one) in previous while condition)
Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while condition is satisfied and it prints ‘2’. (variable ‘i’ is decremented by ‘1’(one) in previous while condition)
Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while condition is satisfied and it prints ‘1’. (variable ‘i’ is decremented by ‘1’(one) in previous while condition)
Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while condition is satisfied and it prints ‘0’. (variable ‘i’ is decremented by ‘1’(one) in previous while condition)
Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while condition is satisfied and it prints ‘-1’. (variable ‘i’ is decremented by ‘1’(one) in previous while condition)
Loop 7: while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.
The output of first while loop is 4,3,2,1,0,-1

Step 2: Then the value of variable i is initialized to ‘5’ Then it prints a new line character(\n).
See the above Loop 1 to Loop 7 .
The output of second while loop is 4,3,2,1,0,-1
Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable ‘i’ is decremented to ‘-1’ by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.
Hence the output of the program is
4,3,2,1,0,-1
4,3,2,1,0,-1

4.Will the program compile successfully?

#include<stdio.h>
int main()
{
    #ifdef NOTE
        int a;
        a=10;
    #else
        int a;
        a=20;
    #endif
    printf("%d\n", a);
    return 0;
}

Explanation:

Yes, this program will compile and run successfully and prints 20.

The macro #ifdef NOTE evaluates the given expression to 1. If satisfied it executes the #ifdef block statements. Here #ifdef condition fails because the Macro NOTE is nowhere declared.

Hence the #else block gets executed, the variable a is declared and assigned a value of 20.

printf("%d\n", a); It prints the value of variable a 20.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值