C - Operators Practice Test.2

  1. What is the purpose of the conditional operator (ternary operator) in C?

    A) To perform bitwise operations

    B) To perform conditional branching

    C) To concatenate strings

    D) To increment a variable

    Answer: B) To perform conditional branching

  2. What is the syntax of the conditional operator in C?

    A) `if (condition) { expression1; } else { expression2; }`

    B) `condition ? expression1 : expression2`

    C) `switch (condition) { case value1: expression1; break; default: expression2; }`

    D) `for (initialization; condition; update) { expression; }`

    Answer: B) `condition ? expression1 : expression2`

  3. In the expression `x = (y > 10) ? 20 : 30;`, what value will be assigned to `x` if `y` is greater than 10?

    A) 10

    B) 20

    C) 30

    D) It depends on the value of `y`

    Answer: B) 20

  4. What is the result of the expression `(x > 5) ? "Yes" : "No"` in C if `x` is equal to 3?

    A) "Yes"

    B) "No"

    C) 3

    D) It will result in a compilation error

    Answer: B) "No"

  5. The conditional operator is also known as:

    A) Ternary operator

    B) Unary operator

    C) Binary operator

    D) Arithmetic operator

    Answer: A) Ternary operator

  6. Which of the following statements about the conditional operator in C is true?

    A) It can only be used with integer values.

    B) It can replace the `if-else` statement for simple conditional expressions.

    C) It can only be used with floating-point numbers.

    D) It can only be used in loops.

    Answer: B) It can replace the `if-else` statement for simple conditional expressions.

  7. In the expression `(x < 0) ? -x : x`, what is the purpose of the conditional operator?

    A) To negate `x` if it is less than 0

    B) To increment `x` if it is less than 0

    C) To decrement `x` if it is greater than 0

    D) To double `x` if it is less than 0

    Answer: A) To negate `x` if it is less than 0

  8. What is the data type of the result of a conditional operator expression in C?

    A) int

    B) float

    C) char

    D) It depends on the data types of the operands

    Answer: D) It depends on the data types of the operands

  9. Can the conditional operator be nested within another conditional operator in C?

    A) Yes, it can be nested without any restrictions.

    B) No, nesting is not allowed.

    C) Yes, but only one level of nesting is allowed.

    D) Yes, but it requires special syntax.

    Answer: A) Yes, it can be nested without any restrictions.

  10. What is the result of the expression `(x == y) ? a : b` in C if `x` is equal to `y`?

    A) `a`

    B) `b`

    C) It will result in a compilation error.

    D) It will throw a runtime error.

    Answer: A) `a`

  11. What is type casting in C?

    A) Converting one data type to another

    B) Converting a string to an integer

    C) Converting a character to a float

    D) Converting an array to a pointer

    Answer: A) Converting one data type to another

  12. Which operator is used for explicit type casting in C?

    A) &

    B) !

    C) *

    D) (type)

    Answer: D) (type)

  13. What is the purpose of type casting?

    A) To perform arithmetic operations

    B) To change the memory location of a variable

    C) To change the data type of a variable

    D) To concatenate strings

    Answer: C) To change the data type of a variable

  14. Which of the following is an example of implicit type casting?

    A) `int x = 5.0;`

    B) `float y = (float)7;`

    C) `char c = 'A';`

    D) `double z = 3.14;`

    Answer: A) `int x = 5.0;`

  15. What is the result of the expression `(int)5.8` after type casting to an integer?

    A) 5.0

    B) 5.8

    C) 5

    D) 6

    Answer: C) 5

  16. What type casting is performed in the expression `double result = (double)(x + y);`?

    A) Implicit type casting

    B) Explicit type casting

    C) No type casting is performed

    D) Pointer type casting

    Answer: B) Explicit type casting

  17. When might you need to use type casting in C?

    A) When printing output

    B) When declaring variables

    C) When performing arithmetic with mixed data types

    D) When including header files

    Answer: C) When performing arithmetic with mixed data types

  18. Which function is used for type casting in C?

    A) `cast()`

    B) `convert()`

    C) `malloc()`

    D) `scanf()`

    Answer: None of the options. Type casting is done using the syntax (type).

  19. What is the data type of the result after casting a float to an int in C?

    A) int

    B) float

    C) double

    D) char

    Answer: A) int

  20. What does the following code snippet do?

                    
                        char ch = 'A';
                        int num = (int)ch;
                    
                

    A) Converts 'A' to 'a'

    B) Converts 'A' to its ASCII value (65)

    C) Converts 'A' to 1

    D) Converts 'A' to 10

    Answer: B) Converts 'A' to its ASCII value (65)

  21. What does the `sizeof` operator in C return?

    A) The value of a variable

    B) The memory address of a variable

    C) The size in bytes of a data type or object

    D) The square root of a number

    Answer: C) The size in bytes of a data type or object

  22. How is the result of the `sizeof` operator usually represented in C?

    A) As a hexadecimal value

    B) As a binary value

    C) As a decimal value

    D) As a floating-point value

    Answer: C) As a decimal value

  23. What is the result of `sizeof(int)` in most C implementations?

    A) 1 byte

    B) 2 bytes

    C) 4 bytes

    D) 8 bytes

    Answer: C) 4 bytes

  24. How can you determine the size of a specific variable, `x`, using the `sizeof` operator?

    A) `sizeof(x)`

    B) `size(x)`

    C) `lengthof(x)`

    D) `memoryof(x)`

    Answer: A) `sizeof(x)`

  25. What is the result of `sizeof(char)` in C?

    A) 1 byte

    B) 2 bytes

    C) 4 bytes

    D) 8 bytes

    Answer: A) 1 byte

  26. Which header file should be included to use the `sizeof` operator in C?

    A) ``

    B) ``

    C) ``

    D) ``

    Answer: D) ``

  27. What does the following C code snippet return?

                   
                        int arr[10];
                        size_t size = sizeof(arr);
                    
                

    A) The value 10

    B) The size of the `int` data type

    C) The size of the `arr` array in bytes

    D) The total number of elements in the array

    Answer: C) The size of the `arr` array in bytes

  28. How can you find the size of a user-defined structure named `MyStruct`?

    A) `size(MyStruct)`

    B) `sizeof(MyStruct)`

    C) `lengthof(MyStruct)`

    D) `structsize(MyStruct)`

    Answer: B) `sizeof(MyStruct)`

  29. What is the result of `sizeof(float)` in C?

    A) 1 byte

    B) 2 bytes

    C) 4 bytes

    D) 8 bytes

    Answer: C) 4 bytes

  30. When is the size returned by `sizeof` evaluated in C?

    A) At compile time

    B) At runtime

    C) At link time

    D) At preprocessing time

    Answer: A) At compile time

  31. What is the purpose of the comma operator in C?

    A) It separates function arguments

    B) It performs bitwise operations

    C) It separates statements and evaluates them from left to right

    D) It concatenates strings

    Answer: C) It separates statements and evaluates them from left to right

  32. In an expression like `x = (a, b);`, what will be the value of `x`?

    A) The value of `a`

    B) The value of `b`

    C) The result of the last expression, which is `b`

    D) The result of the first expression, which is `a`

    Answer: C) The result of the last expression, which is `b`

  33. What does the following expression evaluate to: `(5, 7)`?

    A) 5

    B) 7

    C) 12

    D) 35

    Answer: B) 7

  34. In which situations might the comma operator be useful in C programming?

    A) When declaring variables

    B) When performing mathematical calculations

    C) When writing conditional statements

    D) When evaluating multiple expressions and discarding intermediate results

    Answer: D) When evaluating multiple expressions and discarding intermediate results

  35. What is the precedence of the comma operator in C?

    A) Highest precedence

    B) Medium precedence

    C) Lowest precedence

    D) It depends on the context

    Answer: C) Lowest precedence

  36. What is the purpose of using parentheses in expressions involving the comma operator?

    A) To change the order of evaluation

    B) To indicate multiplication

    C) To separate function arguments

    D) Parentheses are not needed with the comma operator

    Answer: A) To change the order of evaluation

  37. Which of the following statements is true regarding the comma operator?

    A) It can only be used with integers

    B) It can be overloaded in C++

    C) It cannot be used inside loops

    D) It always returns the first operand's value

    Answer: B) It can be overloaded in C++

  38. What does the following code snippet do?

                    
                        int x = 5, y = 10, z;
                        z = (x++, y++);
                    
                

    A) Assigns the value of `x` to `z`

    B) Assigns the value of `y` to `z`

    C) Assigns the value of `x` to `z` after incrementing `x`

    D) Assigns the value of `y` to `z` after incrementing `y`

    Answer: D) Assigns the value of `y` to `z` after incrementing `y`

  39. When might you use the comma operator in a for loop?

    A) To iterate over an array

    B) To skip loop iterations

    C) To combine multiple loop control variables

    D) The comma operator is not used in for loops

    Answer: C) To combine multiple loop control variables

  40. What is the value of the following expression: `(3, 5, 7)`?

    A) 3

    B) 5

    C) 7

    D) The result of the last expression, which is 7

    Answer: D) The result of the last expression, which is 7

  41. In C, which operator has the highest precedence?

    A) Assignment operator (`=`)

    B) Logical OR operator (`||`)

    C) Bitwise XOR operator (`^`)

    D) Conditional operator (`? :`)

    Answer: D) Conditional operator (`? :`)

  42. What is the associativity of the addition operator (`+`) in C?

    A) Left-to-right

    B) Right-to-left

    C) It depends on the context

    D) There is no addition operator in C

    Answer: A) Left-to-right

  43. In the expression `a * b + c`, which operator is evaluated first?

    A) Multiplication operator (`*`)

    B) Addition operator (`+`)

    C) Division operator (`/`)

    D) Modulus operator (`%`)

    Answer: A) Multiplication operator (`*`)

  44. What is the result of the expression `5 + 3 * 2` in C?

    A) 16

    B) 11

    C) 10

    D) 26

    Answer: B) 11

  45. What is the precedence of the logical AND operator (`&&`) in C?

    A) Highest precedence

    B) Medium precedence

    C) Lowest precedence

    D) It depends on the context

    Answer: A) Highest precedence

  46. In the expression `a = b = c`, what is the value of `c`?

    A) The value of `a`

    B) The value of `b`

    C) The result of the assignment `a = b`

    D) Undefined

    Answer: B) The value of `b`

  47. What is the associativity of the equality operator (`==`) in C?

    A) Left-to-right

    B) Right-to-left

    C) It depends on the context

    D) There is no equality operator in C

    Answer: A) Left-to-right

  48. In C, what is the result of the expression `3 / 0`?

    A) 0

    B) 1

    C) Compiler error

    D) Undefined behavior

    Answer: D) Undefined behavior

  49. What is the precedence of the bitwise shift operator (`<<` and `>>`) in C?

    A) Highest precedence

    B) Medium precedence

    C) Lowest precedence

    D) It depends on the context

    Answer: B) Medium precedence

  50. In the expression `x && y || z`, which logical operator is evaluated first?

    A) Logical AND operator (`&&`)

    B) Logical OR operator (`||`)

    C) Logical NOT operator (`!`)

    D) All operators are evaluated simultaneously

    Answer: A) Logical AND operator (`&&`)