3.1 Introduction to C programming

3.1 Introduction to C programming:

 

C TOKENS

 

Which of the following is NOT a valid C token?

a) Identifier

b) Keyword

c) Comment

d) Variable


 

Answer: c) Comment

 

Which type of token is used to represent a function or variable name in C?

a) Identifier

b) Keyword

c) Operator

d) Punctuator


 

Answer: a) Identifier

 

Which of the following is a keyword in C?

a) String

b) Float

c) Class

d) Print

 

Answer: b) Float


 

What type of token is represented by the symbol "+" in C?

a) Identifier

b) Keyword

c) Operator

d) Punctuator

Answer: c) Operator


 

Which of the following is a valid integer constant in C?

a) 3.14

b) 'A'

c) "Hello"

d) 42


 

Answer: d) 42


 

What type of token is represented by the symbol ";" in C?

a) Identifier

b) Keyword

c) Operator

d) Punctuator


 

Answer: d) Punctuator


 

Which of the following is a valid string literal in C?

a) 'Hello'

b) "World"

c) 3.14

d) 42


 

Answer: b) "World"

 

Which token is used to assign a value to a variable in C?

a) Identifier

b) Keyword

c) Operator

d) Punctuator

Answer: c) Operator


 

Which of the following is a valid character constant in C?

a) 3.14

b) 'A'

c) "Hello"

d) 42

 

Answer: b) 'A'


 

What type of token is represented by the symbol "{" in C?

a) Identifier

b) Keyword

c) Operator

d) Punctuator


 

Answer: d) Punctuator

 

OPERATORS

 

Which operator in C is used for assignment?

a) =

b) ==

c) +=

d) /


 

Answer: a) =

 

2.  Which operator in C is used for equality comparison?

a) =

b) ==

c) !=

d) >

Answer: b) ==


 

3. Which operator in C is used for not equal to comparison?

a) =

b) ==

c) !=

d) >



 

Answer: c) !=

 

4. Which operator in C is used for greater than comparison?

a) =

b) ==

c) !=

d) >


 

Answer: d) >

 

5. Which operator in C is used for logical AND?

a) &&

b) ||

c) !

d) &


 

Answer: a) &&

 

6. Which operator in C is used for logical OR?

a) &&

b) ||

c) !

d) &

 

Answer: b) ||


 

FORMATTED/UNFORMATTED INPUT /OUTPUT

 

Which function in C is used to read formatted input from the standard input (keyboard)?

a) scanf()

b) gets()

c) read()

d) fgets()


 

Answer: a) scanf()


 

Which function in C is used to read unformatted input from the standard input (keyboard)?

a) scanf()

b) gets()

c) read()

d) fgets()


 

Answer: b) gets()


 

Which function in C is used to write formatted output to the standard output (console)?

a) printf()

b) puts()

c) write()

d) fputs()


 

Answer: a) printf()



 

Which function in C is used to write unformatted output to the standard output (console)?

a) printf()

b) puts()

c) write()

d) fputs()


 

Answer: b) puts()


 

Which function in C is used to read a line of text (including spaces) from a file?

a) scanf()

b) gets()

c) read()

d) fgets()


 

Answer: d) fgets()


 

Which function in C is used to write a line of text to a file?

a) printf()

b) puts()

c) write()

d) fputs()


 

Answer: d) fputs()


 

Which function in C is considered unsafe for reading input due to potential buffer overflow?

a) scanf()

b) gets()

c) read()

d) fgets()

 

Answer: b) gets()


 

Which function in C is used to read formatted input from a file?

a) scanf()

b) gets()

c) read()

d) fscanf()


 

Answer: d) fscanf()


 

Which function in C is used to write formatted output to a file?

a) printf()

b) puts()

c) write()

d) fprintf()


 

Answer: d) fprintf()


 

Which function in C is used to read a single character from the standard input?

a) getchar()

b) getc()

c) fgetc()

d) read()

 

Answer: a) getchar()


 

CONTROL STATEMENT 


 

Which control statement in C is used to perform a certain set of statements repeatedly as long as a condition is true?

a) if-else statement

b) switch statement

c) for loop

d) do-while loop


 

Answer: d) do-while loop



 

Which control statement in C is used to select one of many code blocks to be executed based on the value of an expression?

a) if-else statement

b) switch statement

c) for loop

d) while loop



 

Answer: b) switch statement



 

Which control statement in C is used to perform a certain set of statements repeatedly until a condition becomes false?

a) if-else statement

b) switch statement

c) if statement

d) while loop


 

Answer: d) while loop




 

Which control statement in C is used to terminate the current iteration of a loop and proceed to the next iteration?

a) break statement

b) continue statement

c) return statement

d) exit statement


 

Answer: b) continue statement


 

Which control statement in C is used to exit from a loop or switch statement prematurely?

a) break statement

b) continue statement

c) return statement

d) exit statement


 

Answer: a) break statement


 

Which control statement in C is used to return a value from a function?

a) break statement

b) continue statement

c) return statement

d) exit statement


 

Answer: c) return statement

 

Which control statement in C is used to jump to a specific point in the code?

a) break statement

b) continue statement

c) return statement

d) goto statement

 

Answer: d) goto statement


 

Which control statement in C is used to execute a set of statements at least once before checking the loop condition?

a) if-else statement

b) switch statement

c) for loop

d) do-while loop



 

Answer: d) do-while loop

 

LOOPING IN C

 

Which loop in C is used when the number of iterations is known beforehand?

a) for loop

b) while loop

c) do-while loop

d) switch loop



 

Answer: a) for loop



 

Which control statement in C is used when the number of iterations is not known beforehand?

a) for loop

b) while loop

c) if-else

d) switch 


 

Answer: b) while loop

Which loop in C is guaranteed to execute the loop body at least once?

a) for loop

b) while loop

c) do-while loop

d) switch loop


 

Answer: c) do-while loop

 

What is the purpose of the loop control variable in a for loop?

a) To determine the loop condition

b) To keep track of the loop iterations

c) To store the loop body statements

d) To control the loop termination



 

Answer: b) To keep track of the loop iterations


 

What is the structure of a for loop in C?

a) for (initialization; condition; increment/decrement)

b) for (condition; increment/decrement; initialization)

c) for (condition; initialization; increment/decrement)

d) for (increment/decrement; initialization; condition)



 

Answer: a) for (initialization; condition; increment/decrement)



 

What is the purpose of the loop condition in a while loop?

a) To determine the loop termination

b) To control the loop increment/decrement

c) To initialize the loop control variable

d) To execute the loop body

Answer: a) To determine the loop termination

 

What is the structure of a while loop in C?

a) while (condition) { }

b) while { condition }

c) while { }

d) while (condition) :

 

Answer: a) while (condition) { }



 

What is the purpose of the loop condition in a do-while loop?

a) To determine the loop termination

b) To control the loop increment/decrement

c) To initialize the loop control variable

d) To execute the loop body



 

Answer: a) To determine the loop termination


 

What is the structure of a do-while loop in C?

a) do { } while (condition);

b) do while (condition) { };

c) do while { condition } ;

d) do { } (condition) ;

 

Answer: a) do { } while

 

USER DEFINED FUNCTIONS

What is the purpose of a user-defined function in C?

a. To perform complex calculations

b. To reuse code and improve modularity

c. To declare variables

d. To handle input/output operations

Answer: b. To reuse code and improve modularity



 

How is a user-defined function declared in C?

a. By using the keyword "function"

b. By specifying the return type, function name, and parameters

c. By using the keyword "define"

d. By declaring it inside the main function



 

Answer: b. By specifying the return type, function name, and parameters



 

Which keyword is used to define a function in C?

a. define

b. function

c. void

d. Return



 

Answer: c. void


 

Which type of function does not return any value in C?

a. Void function

b. Int function

c. Char function

d. Float function



 

Answer: a. Void function


 

What is the purpose of a function prototype in C?

a. To define the function body

b. To declare the function before it is used

c. To specify the return type of the function

d. To pass arguments to the function



 

Answer: b. To declare the function before it is used


 

How are function arguments passed in C?

a. By value

b. By reference

c. By pointer

d. By name


 

Answer: a. By value


 

What is the keyword used to exit a function and return a value in C?

a. break

b. return

c. exit

d. Quit



 

Answer: b. Return

 

Can a function call itself in C?

a. Yes, it is called a recursive function

b. No, it is not allowed

c. Only if the function is declared as void

d. Only if the function is declared as static

Answer: a. Yes, it is called a recursive function



 

What is the maximum number of parameters a function can have in C?

a. 10

b. 32

c. 64

d. There is no maximum limit



 

Answer: d. There is no maximum limit

 

Which statement is used to call a user-defined function in C?

a. execute function

b. call function

c. invoke function

d. Function



 

Answer: b. call function

 

RECURSIVE FUNCTION

 

What is a recursive function in C?

a. A function that calls itself

b. A function that uses loops instead of recursion

c. A function that accepts multiple arguments

d. A function that returns multiple values



 

Answer: a. A function that calls itself



 

What is the base case in a recursive function?

a. The condition that determines when the function should terminate

b. The initial value of the function's variables

c. The maximum number of recursive calls allowed

d. The statement that calls the function recursively



 

Answer: a. The condition that determines when the function should terminate




 

Which of the following is an advantage of using recursive functions?

a. Recursive functions are always more efficient than iterative functions

b. Recursive functions make code easier to understand and maintain

c. Recursive functions consume less memory compared to iterative functions

d. Recursive functions can solve any problem, regardless of complexity




 

Answer: b. Recursive functions make code easier to understand and maintain



 

What happens if a recursive function does not have a base case?

a. The function will terminate with an error

b. The function will run indefinitely and may cause a stack overflow

c. The function will return the value of the last recursive call

d. The function will automatically generate a default base case




 

Answer: b. The function will run indefinitely and may cause a stack overflow

 

When should you use a recursive function instead of an iterative solution?

a. When the problem can be divided into smaller subproblems

b. When efficiency is the top priority

c. When the problem requires a large number of repetitive operations

d. When the problem involves complex mathematical calculations



 

Answer: a. When the problem can be divided into smaller subproblems



 

Which data structure is typically used to implement recursion in C?

a. Arrays

b. Linked lists

c. Stacks

d. Queues



 

Answer: c. Stacks



 

What is the disadvantage of using recursive functions?

a. Recursive functions are harder to debug than iterative functions

b. Recursive functions can only solve simple problems

c. Recursive functions are slower compared to iterative functions

d. Recursive functions can lead to infinite loops if not implemented correctly



 

Answer: a. Recursive functions are harder to debug than iterative functions



 

What is the maximum depth of recursion in C?

a. It depends on the specific compiler and system

b. 100 levels

c. 1000 levels

d. There is no maximum depth of recursion in C



 

Answer: a. It depends on the specific compiler and system


 

ARRAY(1-D,2-D,MULTIDIMENSIONAL)

 

What is an array in C?

a. A variable that can hold multiple values of the same data type

b. A function that performs mathematical operations on numbers

c. A loop that iterates over a sequence of elements

d. A data structure used to store complex data types



 

Answer: a. A variable that can hold multiple values of the same data type



 

How do you declare a 1-dimensional array in C?

a. int array[size]

b. array[size]

c. int[size] array

d. size int array[]



 

Answer: a. int array[size]



 

What is the index range of a 1-dimensional array with N elements?

a. 0 to N-1

b. 1 to N

c. -N to N

d. -N+1 to N



 

Answer: a. 0 to N-1



 

How do you access an element in a 1-dimensional array in C?

a. array(index)

b. array[index]

c. array{index}

d. array.index


 

Answer: b. array[index]




 

How do you declare a 2-dimensional array in C?

a. int array[rows][columns]

b. array[rows][columns]

c. int[rows][columns] array

d. rows int[columns] array



 

Answer: a. int array[rows][columns]


 

What is the index range of a 2-dimensional array with R rows and C columns?

a. 0 to R-1, 0 to C-1

b. 1 to R, 1 to C

c. -R to R, -C to C

d. -R+1 to R, -C+1 to C



 

Answer: a. 0 to R-1, 0 to C-1



 

How do you access an element in a 2-dimensional array in C?

a. array(row, column)

b. array[row][column]

c. array{row, column}

d. array.row.column



 

Answer: b. array[row][column]


 

What is the maximum number of dimensions allowed for an array in C?

a. 1

b. 2

c. 3

d. There is no specific limit



 

Answer: d. There is no specific limit

 

How do you declare a multidimensional array in C?

a. int array[dim1][dim2]...[dimN]

b. array[dim1][dim2]...[dimN]

c. int[dim1][dim2]...[dimN] array

d. dim1 int[dim2]...[dimN] array


 

Answer: a. int array[dim1][dim2]...[dimN]



 

How do you access an element in a multidimensional array in C?

a. array(index1, index2, ..., indexN)

b. array[index1][index2]...[indexN]

c. array{index1, index2, ..., indexN}

d. array.index1.index2...indexN


 

Answer: b. array[index1][index2]...[indexN]


 

STRING MANIPULATION 

 

Which library should be included to perform string manipulation in C?

a. stdlib.h

b. math.h

c. string.h

d. Stdio.h


 

Answer: c. string.h



 

Which function is used to calculate the length of a string in C?

a. strlen()

b. strcpy()

c. strcat()

d. strcmp()


 

Answer: a. strlen()

 

Which function is used to copy one string to another in C?

a. strlen()

b. strcpy()

c. strcat()

d. strcmp()


 

Answer: b. strcpy()


 

Which function is used to concatenate two strings in C?

a. strlen()

b. strcpy()

c. strcat()

d. strcmp()



 

Answer: c. strcat()


 

Which function is used to compare two strings in C?

a. strlen()

b. strcpy()

c. strcat()

d. strcmp()



 

Answer: d. strcmp()

 

How do you declare a string in C?

a. char str[];

b. char[] str;

c. string str;

d. char* str;

 

Answer: a. char str[];


 

How do you input a string from the user in C?

a. scanf("%s", str);

b. gets(str);

c. fgets(str, sizeof(str), stdin);

d. All of the above



 

Answer: d. All of the above



 

What is the character used to mark the end of a string in C?

a. Period (.)

b. Exclamation mark (!)

c. Null character (\0)

d. Dollar sign ($)



 

Answer: c. Null character (\0)