C++ MCQs Part-2

21–30: Functions and Arrays

  1. Which is the correct syntax to define a function in C++?
    a) function name() {}
    b) void name() {}
    c) def name():
    d) fun name() {}
    Answer: b

  2. Which is the correct way to pass a variable by reference?
    a) void func(int x)
    b) void func(int* x)
    c) void func(int &x)
    d) void func(ref x)
    Answer: c

  3. What is the index of the first element in an array?
    a) 1
    b) 0
    c) -1
    d) It depends
    Answer: b

  4. What is the correct way to declare a 1D array of 10 integers?
    a) int arr[10];
    b) int[10] arr;
    c) arr int[10];
    d) array[10] int;
    Answer: a

  5. What happens if you access an array out of bounds in C++?
    a) Compiler error
    b) Runtime error
    c) Undefined behavior
    d) Segmentation fault
    Answer: c

  6. Which of these is not a valid return type of a function in C++?
    a) void
    b) int
    c) array
    d) char
    Answer: c

  7. Which of the following denotes function overloading?
    a) Functions with same name, different parameters
    b) Functions with different names
    c) Functions inside classes
    d) None
    Answer: a

  8. What does sizeof(arr) return in C++ if arr is an array of 5 integers?
    a) 5
    b) 10
    c) 20 (if int = 4 bytes)
    d) Compiler error
    Answer: c

  9. What is recursion?
    a) A function that calls itself
    b) A function that loops
    c) A loop inside a function
    d) None
    Answer: a

  10. Which header file is required for using mathematical functions like sqrt()?
    a) <math.h>
    b) <cmath>
    c) Both
    d) None
    Answer: c

Post a Comment

0 Comments