Decode The Code By Iste(Indian Society For Technical Education)

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Shivam Hada
S
Shivam Hada
Community Contributor
Quizzes Created: 1 | Total Attempts: 336
Questions: 15 | Attempts: 336

SettingsSettingsSettings
Decode The Code By Iste(Indian Society For Technical Education) - Quiz


Questions and Answers
  • 1. 

    1. What will be the output of the following C code?     #include <stdio.h>     int main()     {         int a = 10, b = 10;         if (a = 5)         b--;         printf("%d, %d", a, b--);       }

    • A.

      A=10, b = 9

    • B.

      A=10, b = 8

    • C.

      A = 5, b = 9

    • D.

      A = 5, b = 8

    Correct Answer
    C. A = 5, b = 9
    Explanation
    In the given code, the variable "a" is assigned a value of 10 and the variable "b" is also assigned a value of 10. The if statement checks if "a" is equal to 5, but it also assigns the value 5 to "a" instead of comparing. Therefore, the condition is true and the block of code inside the if statement is executed. As a result, the value of "a" becomes 5 and the value of "b" is decremented by 1. Finally, the printf statement prints the values of "a" and "b", which are 5 and 9 respectively.

    Rate this question:

  • 2. 

    Can you combine the following two statements into one? char *p; p = (char*) malloc(100);

    • A.

      Char p = *malloc(100);

    • B.

      Char *p = (char) malloc(100);

    • C.

      Char *p = (char*)malloc(100);

    • D.

      Char *p = (char *)(malloc*)(100);

    Correct Answer
    C. Char *p = (char*)malloc(100);
    Explanation
    The correct answer is "char *p = (char*)malloc(100)". This answer combines the two statements into one by declaring a pointer variable 'p' of type char and assigning it the address returned by the malloc function, which allocates 100 bytes of memory. The typecast (char*) is used to convert the returned address to a char pointer type.

    Rate this question:

  • 3. 

    3. What will be the output of the following C code?
    1.     #include <stdio.h>
    2.     int main()
    3.     {
    4.         char *str = "hello world";
    5.         char strary[] = "hello world";
    6.         printf("%d %d\n", sizeof(str), sizeof(strary));
    7.         return 0;
    8.     }

    • A.

      11 11

    • B.

      12 12

    • C.

      4 12

    • D.

      4 11

    Correct Answer
    C. 4 12
    Explanation
    The code declares a pointer variable `str` and an array `strary`, both initialized with the string "hello world". The `sizeof` operator is then used to determine the size of `str` and `strary`. Since `str` is a pointer, its size is determined by the size of a pointer variable (which is typically 4 bytes on a 32-bit system). On the other hand, `strary` is an array, so its size is determined by the number of elements multiplied by the size of each element (which is 1 byte for a `char`). Therefore, the output of the code is "4 12".

    Rate this question:

  • 4. 

     What will be the output of the following C code?
    1.     #include <stdio.h>
    2.     int main()
    3.     {
    4.         int ary[2][3];
    5.         ary[][] = {{1, 2, 3}, {4, 5, 6}};
    6.         printf("%d\n", ary[1][0]);
    7.     }

    • A.

      Compile time error

    • B.

      4

    • C.

      1

    • D.

      2

    Correct Answer
    A. Compile time error
    Explanation
    The given code will result in a compile-time error because the array initialization is incorrect. In C, when initializing a 2D array, we need to specify the size of the second dimension. In this case, the correct initialization would be: int ary[2][3] = {{1, 2, 3}, {4, 5, 6}}. However, in the given code, the second dimension is left empty, resulting in a compile-time error.

    Rate this question:

  • 5. 

     How many times the program will print "Program" ? #include<stdio.h> int main() {     printf("Program");     main();     return 0; }

    • A.

      Infinite times

    • B.

      32767 time

    • C.

      65535 times

    • D.

      Till stack overflows

    Correct Answer
    D. Till stack overflows
    Explanation
    The program will print "Program" until the stack overflows. This is because the main function is recursively calling itself without any condition to stop the recursion. As a result, the program will continue to print "Program" indefinitely until the stack memory is exhausted and a stack overflow occurs.

    Rate this question:

  • 6. 

    Which variable has the longest scope in the following C code?
    1.     #include <stdio.h>
    2.     int b;
    3.     int main()
    4.     {
    5.         int c;
    6.         return 0;
    7.     }
    8.     int a;

    • A.

      A

    • B.

      B

    • C.

      C

    • D.

      Both a and b

    Correct Answer
    B. B
    Explanation
    The variable with the longest scope in the given C code is "b". This is because "b" is declared outside of any function, making it a global variable. Global variables have a scope that extends throughout the entire program, meaning they can be accessed and modified by any function. In contrast, the variables "a" and "c" are declared within the main function, making their scope limited to that function only. Therefore, "b" has the longest scope.

    Rate this question:

  • 7. 

    What is the output of the following C code if there is no error in stream fp?
    1.     #include <stdio.h>
    2.     int main()
    3.     {
    4.         FILE *fp;
    5.         fp = fopen("newfile", "w");
    6.         printf("%d\n", ferror(fp));
    7.         return 0;
    8.     }

    • A.

      Compilation error

    • B.

      0

    • C.

      1

    • D.

      Any nonzero value

    Correct Answer
    B. 0
    Explanation
    The code opens a file named "newfile" in write mode using the fopen function. If there is no error in opening the file, the value of fp will not be NULL. Then, the code uses the ferror function to check if there is any error in the stream fp. Since there is no error in opening the file, the ferror function will return 0. Finally, the code prints the value returned by ferror, which is 0.

    Rate this question:

  • 8. 

    What will be the output of the following C code? #include<stdio.h> main() {     int n;     n=f1(4);     printf("%d",n); } f1(int x) {     int b;     if(x==1)         return 1;     else         b=x*f1(x-1);         return b; }

    • A.

      24

    • B.

      4

    • C.

      12

    • D.

      10

    Correct Answer
    A. 24
    Explanation
    The given code is a recursive function that calculates the factorial of a number. In the main function, the value of n is assigned the result of calling the f1 function with the argument 4. The f1 function recursively calculates the factorial by multiplying the current number with the factorial of the previous number until x becomes 1. In this case, the factorial of 4 is calculated as 4 * 3 * 2 * 1 = 24. Therefore, the output of the code will be 24.

    Rate this question:

  • 9. 

    What will be the output of the following C code?
    1.     #include <stdio.h>
    2.     int main()
    3.     {
    4.         printf("%d ", 1);
    5.         goto l1;
    6.         printf("%d ", 2);
    7.         l1:goto l2;
    8.         printf("%d ", 3);
    9.         l2:printf("%d ", 4);
    10.    }

    • A.

      1  4

    • B.

      Compilation error

    • C.

      1  2  4

    • D.

      1  3  4

    Correct Answer
    A. 1  4
    Explanation
    The output of the code will be "1 4". This is because the code uses the goto statement to jump to different parts of the code. The first printf statement prints "1" and then it jumps to the label "l1". The next printf statement is skipped because of the goto statement. Then it jumps to the label "l2" and the final printf statement prints "4". So, the output is "1 4".

    Rate this question:

  • 10. 

    What will be the content of 'file.c' after executing the following program? #include<stdio.h> int main() {     FILE *fp1, *fp2;     fp1=fopen("file.c", "w");     fp2=fopen("file.c", "w");     fputc('A', fp1);     fputc('B', fp2);     fclose(fp1);     fclose(fp2);     return 0; }

    • A.

      A

    • B.

      A B

    • C.

      B B

    • D.

      Error in opening file 'file1.c'

    Correct Answer
    A. A
    Explanation
    The content of 'file.c' after executing the program will be "A". This is because the program opens the file 'file.c' twice, first with mode "w" which truncates the file to zero length, and then writes the character 'A' to it using the file pointer fp1. The file is then closed. When the file is opened again with mode "w" using the file pointer fp2, it truncates the file to zero length again. Therefore, the character 'A' is the only content in the file before it is closed.

    Rate this question:

  • 11. 

    If an unsigned int is 2 bytes wide then, What will be the output of the program ? #include<stdio.h> int main() {     unsigned int a=0xffff;     ~a;     printf("%x\n", a);     return 0; }

    • A.

      Ffff

    • B.

      0000

    • C.

      00ff

    • D.

      Ddfd

    Correct Answer
    A. Ffff
    Explanation
    The program initializes an unsigned int variable 'a' with the hexadecimal value 0xffff. The '~' operator is then applied to 'a', which performs a bitwise complement operation on 'a'. This operation flips all the bits in 'a'. The program then prints the value of 'a' in hexadecimal format using the printf function. Since the bitwise complement operation flips all the bits in 'a', the output will be the complement of 0xffff, which is also 0xffff.

    Rate this question:

  • 12. 

     What will be the output of the following C code?
    1.     #include <stdio.h>
    2.     void main()
    3.     {
    4.         struct student
    5.         {
    6.             int no;
    7.             char name[20];
    8.         };
    9.         struct student s;
    10.         no = 8;
    11.         printf("%d", no);
    12.     }

    • A.

      Nothing

    • B.

      Compile time error

    • C.

      Junk

    • D.

      8

    Correct Answer
    B. Compile time error
    Explanation
    The code will result in a compile time error because the variable "no" is not declared or defined before it is used in the line "no = 8;". Since "no" is a member of the struct "student", it should be accessed using the dot operator with the struct variable "s". Therefore, the correct way to assign a value to "no" would be "s.no = 8;".

    Rate this question:

  • 13. 

    What will be the output of the program? #include<stdio.h> int main() {     int i;     i = printf("How r u\n");     i = printf("%d\n", i);     printf("%d\n", i);     return 0; }

    • A.

      How r u 7 2

    • B.

      How r u 8 2

    • C.

      How r u 1 1

    • D.

      Error: cannot assign printf to variable

    Correct Answer
    B. How r u 8 2
    Explanation
    The program first prints "How r u" and returns the number of characters printed, which is 8. Then it prints the value of i, which is 8. Finally, it prints the value of i again, which is 2.

    Rate this question:

  • 14. 

    What will be the output of the program (16-bit platform)? #include<stdio.h> #include<stdlib.h> int main() {     int *p;     p = (int *)malloc(20);     printf("%d\n", sizeof(p));     free(p);     return 0; }

    • A.

      4

    • B.

      2

    • C.

      8

    • D.

      Garbage Value

    Correct Answer
    B. 2
    Explanation
    The program allocates memory of size 20 bytes using the malloc function and assigns the address of the allocated memory to the pointer variable 'p'. The sizeof(p) function returns the size of the pointer variable 'p' in bytes, which is 2 on a 16-bit platform. This is because the size of a pointer is determined by the architecture of the platform, not the size of the memory it points to. The program then frees the allocated memory using the free function and returns 0.

    Rate this question:

  • 15. 

    #include<stdio.h> #include<stdlib.h> int main() {     union test     {         int i;         float f;         char c;     };     union test *t;     t = (union test *)malloc(sizeof(union test));     t->f = 10.10f;     printf("%f", t->f);     return 0; }

    • A.

      10

    • B.

      Garbage value

    • C.

      10.100000

    • D.

      Error

    Correct Answer
    C. 10.100000
    Explanation
    The correct answer is 10.100000. This is because the code declares a union called "test" which has three members: an integer, a float, and a character. It then dynamically allocates memory for a variable of type "test" using malloc(). The float member "f" is then assigned the value 10.10f. Finally, the value of "f" is printed using printf(). Since "f" was assigned the value 10.10f, the output will be 10.100000.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 30, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 17, 2020
    Quiz Created by
    Shivam Hada
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.