Zoho Programming Questions (PYQ's)

by Jasleen Chhabra | Updated on 23 July 2024

1. What will be the output of the following C code?

#include <stdio.h>

int main() {

    int x = 5;

    int y = x++ * ++x;

    printf("%d", y);

    return 0;

}

a) 25

b) 30

c) 35

d) Undefined behavior

Answer: d) Undefined behavior

Explanation: The expression x++ * ++x leads to undefined behavior because the order of evaluation of the operands is not specified. The variable x is modified twice without an intervening sequence point, which results in undefined behavior according to the C standard.


2. What will be the output of the following C code snippet?

#include <stdio.h>

int main() {

    int a = 10;

    int *ptr;

    ptr = &a;

    printf("%d", *ptr);

    return 0;

}

a) 10

b) Address of a

c) Compilation error

d) Garbage value

Answer: a) 10

Explanation: The pointer ptr is assigned the address of a using ptr = &a;. The *ptr dereferences the pointer to get the value of a, which is 10.


3. What will be the output of the following C code?

#include <stdio.h>

int main() {

    int arr[3] = {1, 2, 3};

    int *p = arr;

    printf("%d", *(p+1));

    return 0;

}

a) 1

b) 2

c) 3

d) Compilation error

Answer: b) 2

Explanation: The pointer p is initialized to point to the first element of arr. The expression *(p+1) accesses the second element of arr, which is 2.


4. Which of the following is the correct way to allocate memory dynamically for an array of 10 integers in C?

a) int *arr = malloc(10);

b) int *arr = malloc(10 * sizeof(int));

c) int *arr = (int *)malloc(10);

d) int *arr = (int *)malloc(10 * sizeof(int));

Answer: d) int *arr = (int *)malloc(10 * sizeof(int));

Explanation: The function malloc allocates memory in bytes. To allocate memory for 10 integers, you need 10 * sizeof(int) bytes. The cast (int *) is necessary in C++ but optional in C.


5. What will be the output of the following C code?

#include <stdio.h>

int main() {

    int a = 5, b = 10;

    int c = a > b ? a : b;

    printf("%d", c);

    return 0;

}

a) 5

b) 10

c) 15

d) Compilation error

Answer: b) 10

Explanation: The ternary operator ? : evaluates the condition a > b. Since 5 is not greater than 10, the expression evaluates to b, which is 10.


6. What is the value of the variable sum after executing the following C code?

#include <stdio.h>

int main() {

    int sum = 0;

    for (int i = 1; i <= 5; i++) {

        sum += i;

    }

    printf("%d", sum);

    return 0;

}

a) 5

b) 10

c) 15

d) 20

Answer: c) 15

Explanation: The for loop iterates from 1 to 5, adding each value of i to sum. The sum of the first five natural numbers is 1 + 2 + 3 + 4 + 5 = 15.


7. Which of the following statements about pointers is true?

a) A pointer can hold the address of any data type

b) Pointers cannot be compared

c) The size of a pointer is always equal to the size of the data type it points to

d) Pointers cannot be incremented or decremented

Answer: a) A pointer can hold the address of any data type

Explanation: A pointer is a variable that stores the memory address of another variable. It can hold the address of any data type.


8. What will be the output of the following C code?

#include <stdio.h>

#include <stdlib.h>

int main() {

    int *p;

    p = (int *)malloc(sizeof(int));

    *p = 100;

    printf("%d", *p);

    free(p);

    return 0;

}

a) 100

b) 0

c) Compilation error

d) Undefined behavior

Answer: a) 100

Explanation: Memory is dynamically allocated for an integer using malloc. The allocated memory is then assigned the value 100. The value is printed, and the allocated memory is freed.


9. What will be the value of a after executing the following C code?

#include <stdio.h>

int main() {

    int a = 10;

    a = a++ + ++a;

    printf("%d", a);

    return 0;

}

a) 20

b) 21

c) 22

d) Undefined behavior

Answer: d) Undefined behavior

Explanation: The expression a++ + ++a involves modifying a twice without an intervening sequence point, leading to undefined behavior.


10. What will be the output of the following C code?

#include <stdio.h>

int main() {

    int i = 0;

    while (i < 3) {

        printf("%d", i);

        i++;

    }

    return 0;

}

a) 012

b) 123

c) 345

d) Compilation error

Answer: a) 012

Explanation: The while loop prints the value of i starting from 0 and increments i until it is less than 3. The output will be 012.


FAQ

Any Questions?
Look Here.