C Programming Practice - Test 4

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 Nchaudhuri31
N
Nchaudhuri31
Community Contributor
Quizzes Created: 19 | Total Attempts: 8,899
Questions: 20 | Attempts: 105

SettingsSettingsSettings
C Programming Practice - Test 4 - Quiz

. Dear Learner,
This Assessment consists of questions to test your understanding on - C Programming
You may attempt the same and clarify doubts in WP Group. / With your Faculty Member
Regards,
Team SHIVAM


Questions and Answers
  • 1. 

    What is the output? main() { char val[] = {‘a’,’b’,’c’,’\n’,’c’,’\0’}; char *p, *val1, *val2; p = &val[3]; val1 = val; val2 = val; printf(“%d”, ++*p + ++*val1 – 32); }

    • A.

      Non-portable pointer conversion

    • B.

      122

    • C.

      97

    • D.

      77

    Correct Answer
    A. Non-portable pointer conversion
  • 2. 

    What is the output? main() { char *arr = “abc defg”, *x; x = arr; while( *arr != ‘\0’ ) ++*arr++; printf(“%s %s”,arr,x); }

    • A.

      Compiler error

    • B.

      No output

    • C.

      Bcd!efgh

    • D.

      Abc defg

    Correct Answer
    C. Bcd!efgh
    Explanation
    The program initializes a character pointer 'arr' with the string "abc defg" and another character pointer 'x'. 'x' is then assigned the value of 'arr'. The program then enters a while loop which increments the value at the memory location pointed to by 'arr' and increments the pointer 'arr' until it encounters the null character '\0'. This effectively increments each character in the string by 1. After the loop, the program prints the value of 'arr' (which is now "bcd!efgh") and 'x' (which is still "abc defg"). Therefore, the output is "bcd!efgh abc defg".

    Rate this question:

  • 3. 

    What is the output? main() { char far *ptr1, *ptr2; printf(“%d %d”,sizeof(ptr1),sizeof(ptr2)); }

    • A.

      2 2

    • B.

      4 4

    • C.

      2 4

    • D.

      4 2

    Correct Answer
    D. 4 2
    Explanation
    The output of the code is "4 2". The "sizeof" operator is used to determine the size of a data type in bytes. In this case, "ptr1" is declared as a far pointer, which typically requires 4 bytes to store the segment and offset information. However, the size of a pointer can vary depending on the compiler and platform. "ptr2" is declared as a regular pointer, which typically requires 2 bytes. Therefore, the output is 4 for "ptr1" and 2 for "ptr2".

    Rate this question:

  • 4. 

    What is the output? main() { char family[4][20] = { “jesus”, “ram” , “rahim” , “gurunanak” }; char *temp; int count ; temp = family[1]; family[1] = family[2]; // Lvalue required for this line family[2] = temp; // Lvalue required for this line for(count = 0 ;count

    • A.

      Non portable pointer conversion

    • B.

      Compilation error-Lvalue required

    • C.

      jesus rahim ram gurunanak

    • D.

      Jesus ram rahim gurunanak

    Correct Answer
    B. Compilation error-Lvalue required
    Explanation
    The correct answer is "Compilation error-Lvalue required". This is because in the lines "family[1] = family[2]" and "family[2] = temp", the left-hand side of the assignment operator is an array element, which is not a valid lvalue. An lvalue is an expression that can appear on the left-hand side of an assignment. Therefore, a compilation error occurs.

    Rate this question:

  • 5. 

    Int main() { struct Some Name { int size = 5; char tips[] = "LEARN"; }; struct SomeName ss; printf("\n %d %s",ss.size,ss.tips); return 0; }

    • A.

      Structure cannot be defined inside main()

    • B.

      5 LEARN

    • C.

      Structure member can not be initialized

    • D.

      Structure can not have char[]

    Correct Answer
    C. Structure member can not be initialized
    Explanation
    The given code defines a structure named "SomeName" inside the main() function. The structure has two members, "size" and "tips". However, the initialization of structure members inside the structure definition is not allowed. Therefore, the correct answer is "Structure member can not be initialized".

    Rate this question:

  • 6. 

    Main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }

    • A.

      1

    • B.

      0

    • C.

      Compiler error

    • D.

      None of these

    Correct Answer
    C. Compiler error
    Explanation
    The given code will result in a compiler error because the structure `xx` contains a member `name` which is an array of characters. In C, arrays cannot be initialized inside a structure declaration. Therefore, the initialization of `name` with the string "hello" will cause a compiler error.

    Rate this question:

  • 7. 

    Main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); }

    • A.

      2 5 5

    • B.

      A b c

    • C.

      Compiler error

    • D.

      None of these

    Correct Answer
    A. 2 5 5
    Explanation
    The correct answer is 2 5 5.

    In this program, the variable str1 is a pointer to a string literal "abcd", while str2 is an array of characters with a size of 5 (including the null character).

    The sizeof operator returns the size of the variable or type in bytes. Since str1 is a pointer, sizeof(str1) will return the size of a pointer (usually 2 or 4 bytes depending on the system).

    On the other hand, sizeof(str2) will return the size of the array, which is 5 bytes.

    Lastly, sizeof("abcd") will return the size of the string literal "abcd", which is also 5 bytes.

    Rate this question:

  • 8. 

    Void main() { int array[]={10,20,30,40}; printf("%d",-2[array]); }

    • A.

      -30

    • B.

      60

    • C.

      30

    • D.

      Garbage Value

    Correct Answer
    C. 30
    Explanation
    In C programming, array indexing can be done using pointer arithmetic. In this case, the expression "-2[array]" is equivalent to "array[-2]". Since arrays are zero-indexed, this means accessing the element at index -2. However, C does not perform bounds checking, so it accesses the memory location before the start of the array. In this case, it accesses the memory location that stores the value 30. Therefore, the correct answer is 30.

    Rate this question:

  • 9. 

    Main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }

    • A.

      Mmmm aaaa nnnn

    • B.

      Compiler error

    • C.

      Mmm aaa nnn

    • D.

      Garbage Value

    Correct Answer
    A. Mmmm aaaa nnnn
    Explanation
    The given code is a C program that prints a sequence of characters using different methods. The code initializes a character array "s" with the value "man". Then, it enters a for loop that iterates until the null character ('\0') is encountered in the array. Inside the loop, it uses different methods to print the characters.

    The first printf statement uses the array index notation "s[i]" to print the character at index i, followed by three variations of pointer arithmetic to access the same character.

    Therefore, the correct answer is "mmmm aaaa nnnn" as the loop iterates over the characters in the "s" array and prints them four times using different methods.

    Rate this question:

  • 10. 

    What is the the output ? main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j

    • A.

      2 2 2 2 2 3 4 6 5

    • B.

      2 2 2 2 2 2 3 4 6 5

    • C.

      2 2 2 2 2 2 4 6 5

    • D.

      2 2 2 2 2 2 3 4 6

    Correct Answer
    B. 2 2 2 2 2 2 3 4 6 5
  • 11. 

    What is the output ? main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }

    • A.

      0 0 1 3 1

    • B.

      0 1 3 1 0

    • C.

      1 3 1 0 0

    • D.

      0 0 1 1 3

    Correct Answer
    A. 0 0 1 3 1
    Explanation
    The code initializes variables i and j to -1, k to 0, and l to 2. The expression i++&&j++&&k++||l++ is evaluated. Since i is -1, the expression i++ evaluates to false and the rest of the expression is not evaluated. Therefore, i and j remain -1, k is incremented to 1, l is incremented to 3, and m is assigned the value of l (which is 3). Finally, the printf statement prints the values of i, j, k, l, and m, resulting in the output "0 0 1 3 1".

    Rate this question:

  • 12. 

    What is the output ? void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); }

    • A.

      3 3

    • B.

      6 3

    • C.

      6 6

    • D.

      3 6

    Correct Answer
    D. 3 6
    Explanation
    The code first declares a character array "a" with the value "12345\0". The strlen() function is then used to determine the length of the string, which is 5. The variable "i" is assigned the value of 5. The printf() function is used to print "here in 3" followed by the value of "++i", which is 6. Therefore, the output would be "3 6".

    Rate this question:

  • 13. 

    Which of the following doesn’t match an array

    • A.

      Static allocation

    • B.

      Successive memory

    • C.

      Heterogeneity

    • D.

      Fixed size

    Correct Answer
    C. Heterogeneity
    Explanation
    The term "heterogeneity" refers to the state of being diverse or composed of different elements. In the context of arrays, it means that the elements within the array are not of the same type or size. However, arrays typically consist of elements that are of the same type and size, making "heterogeneity" the option that doesn't match.

    Rate this question:

  • 14. 

    Which of the following character is appended with a string

    • A.

      ‘\r’

    • B.

      ‘\n’

    • C.

      ‘\t’

    • D.

      ‘\0’

    Correct Answer
    D. ‘\0’
    Explanation
    The character '\0' is appended with a string. This is because '\0' represents the null character, which is used to indicate the end of a string in C programming. When a '\0' character is appended to a string, it signifies the termination of the string and is used to ensure that string functions can correctly identify the end of the string.

    Rate this question:

  • 15. 

    What is the output ? #include #include int main() { char *str = "x"; char c = 'x'; char ary[1]; ary[0] = c; printf("%d %d", strlen(str), strlen(ary)); return 0; }

    • A.

      1 1

    • B.

      2 1

    • C.

      2 2

    • D.

      1 (undefined value)

    Correct Answer
    D. 1 (undefined value)
    Explanation
    The output of the program is "1 (undefined value)".


    The program declares a character pointer `str` and initializes it with the string "x". It also declares a character `c` and assigns it the value 'x'. Then, it declares a character array `ary` with size 1 and assigns `c` to its first element.

    When the `strlen` function is called on `str`, it returns 1 because it counts the number of characters until it reaches the null terminator '\0'.

    However, when the `strlen` function is called on `ary`, it causes undefined behavior because `ary` is not null-terminated. The `strlen` function will continue reading memory until it finds a null terminator, which may result in accessing memory beyond the bounds of `ary`. The output of `strlen(ary)` is therefore unpredictable and can vary.

    Rate this question:

  • 16. 

    What will be the output? void main() { char c=125; c=c+10; printf(“%d”,c); }

    • A.

      -121

    • B.

      -8

    • C.

      135

    • D.

      Compiler Error

    Correct Answer
    A. -121
    Explanation
    In this code, the variable 'c' is initially assigned the value 125, which is within the range of a char data type. However, when 10 is added to 'c', the resulting value exceeds the range of a char data type. This causes an overflow and the value wraps around to the minimum value of a char data type, which is -128. Therefore, the output will be -121.

    Rate this question:

  • 17. 

    Void main() { int a[]={0,1,2,3,4}; int * p[]={a,a+1,a+2,a+3.a+4}; printf(“%d”,*(*p)); }

    • A.

      A

    • B.

      A+4

    • C.

      0

    • D.

      4

    Correct Answer
    C. 0
    Explanation
    The code declares an array 'a' with 5 elements and a pointer array 'p' with 5 elements. Each element of 'p' is assigned the address of the corresponding element in 'a'. The printf statement dereferences the pointer at index 0 of 'p', which is equivalent to dereferencing the pointer at the beginning of 'a'. Therefore, the output will be the value at the first element of 'a', which is 0.

    Rate this question:

  • 18. 

    An array is a

    • A.

      ADT

    • B.

      Basic datatype

    • C.

      Derived datatype

    • D.

      User defined datatype

    Correct Answer
    C. Derived datatype
    Explanation
    An array is a derived datatype because it is created by combining elements of a basic datatype, such as integers or characters, into a single collection. The array itself can then be used as a datatype to store multiple values of the same type in a contiguous memory space. This allows for efficient access and manipulation of the elements within the array.

    Rate this question:

  • 19. 

    The array name indicates

    • A.

      Const pointer

    • B.

      Pointer to constant

    • C.

      Generic pointer

    Correct Answer
    A. Const pointer
    Explanation
    The term "const pointer" refers to a pointer that points to a constant value. In other words, the value being pointed to cannot be modified through the pointer. This is indicated by the "const" keyword before the pointer name.

    Rate this question:

  • 20. 

    The difference between a character array and string is

    • A.

      Appended ‘\0’ in string

    • B.

      Appended ‘\0’ in character array

    • C.

      %s can’t be used for character array

    • D.

      %s can be used for both

    Correct Answer
    A. Appended ‘\0’ in string
    Explanation
    The correct answer is "appended '\0' in string". This is because in C programming, a string is represented as a character array terminated by a null character ('\0'). This null character indicates the end of the string. On the other hand, a character array may or may not have a null character at the end. The presence of the null character in a string allows for various string manipulation functions to work correctly, while using %s format specifier to print a character array without a null character can result in unpredictable behavior.

    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
  • Feb 17, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jun 11, 2018
    Quiz Created by
    Nchaudhuri31
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.