C Programming Quiz: Variable Names, Output, Constants, sizeof()

IndividualizedPromethium avatar
IndividualizedPromethium
·
·
Download

Start Quiz

Study Flashcards

12 Questions

What is the purpose of the & operator in C?

Address of operator

Which of the following is the correct way to access the fourth element of an array named nums?

nums

What is the output of the following code?

int i = 0; for (; i < 5; i++) { printf("%d ", i); }

0 1 2 3

Which of the following statements is true about the do-while loop in C?

The loop body is executed at least once

What is the correct way to define a function in C that returns an integer and takes two parameters a and b?

int functionName(int a, int b) { }

Which of the following is NOT a valid way to declare a string in C?

string str = "Hello";

Which of the following is NOT a valid C variable name?

123variable

What does the sizeof() operator return in C?

Size of the type

What does the scanf() function do in C?

Reads formatted input

What is the result of the expression 3 / 2 in C?

1.0

Which of the following is a logical operator in C?

||

What is the correct way to declare a constant in C?

const int CONSTANT = 10;

Study Notes

C Variable Names

  • myVariable and variable123 are valid variable names
  • 123variable is not a valid variable name (numbers cannot start a variable name)
  • _variable is a valid variable name

C Output

  • printf("%d", x++); outputs 5 (post-increment x++ returns the original value of x)

Declaring Constants in C

  • const int CONSTANT = 10; is the correct way to declare a constant in C

sizeof() Operator

  • The sizeof() operator returns the size of a type or variable in bytes

Division in C

  • The result of the expression 3 / 2 in C is 1 (integer division truncates the decimal part)

Logical Operators in C

  • || is a logical operator in C (logical OR)

Array Declaration in C

  • int array[10]; is the syntax to declare an array in C with 10 elements

scanf() Function

  • The scanf() function reads formatted input from the standard input

Terminating a C Program

  • return 0; is the correct way to terminate a C program

Conditional Operator

  • x > y ? x : y returns x if x is greater than y, and y otherwise

Header Files in C

  • stdio.h is the header file required for input and output operations in C

break Statement

  • The purpose of the break statement in a loop is to terminate the loop

for Loop Syntax

  • The correct syntax for the for loop in C is for (int i = 0; i < 10; i++)

while Loop Output

  • The output of the code while (i < 5) { printf("%d ", i); i += 2; } is 0 2 4

String Declaration in C

  • char str[] = "Hello"; and char *str = "Hello"; are valid ways to declare a string in C
  • char str = "Hello"; and string str = "Hello"; are not valid ways to declare a string in C

if Statement

  • The output of the code if (x = 6) printf("True"); else printf("False"); is True (assignment x = 6 has a non-zero value, which is considered true)

continue Statement

  • The purpose of the continue statement in a loop is to skip the remaining code in the loop and proceed to the next iteration

Array Access

  • arr is a pointer to the first element of the array, so printf("%d", arr); prints the memory address of the first element (garbage value)

do-while Loop

  • The loop body of a do-while loop is executed at least once before the condition is evaluated

Prefix Increment Operator

  • The prefix increment operator ++i increments i before returning its value, so printf("%d", ++i); outputs 11

Array Access

  • nums[3] is the correct way to access the fourth element of an array named nums

& Operator

  • The & operator is an address of operator, which returns the memory address of a variable

for Loop Output

  • The output of the code for (; i < 5; i++) { printf("%d ", i); } is 0 1 2 3 4

Function Definition in C

  • The correct way to define a function in C that returns an integer and takes two parameters a and b is int functionName(int a, int b) { }

strcmp() Function

  • The strcmp() function compares two strings, returning a negative value if the first string is less than the second, zero if they are equal, and a positive value if the first string is greater than the second.

Test your knowledge of C programming with this quiz! Identify valid variable names, predict the output of code snippets, determine the correct way to declare constants, and understand the purpose of the sizeof() operator.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser