Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

Sure!

Here are 30 multiple-choice questions related to the principles of fundamental programming


concepts using C++, along with their answers:

1. Understanding: Which of the following best defines a variable in C++?

a) A keyword used to declare functions

b) A named storage location that can hold a value

c) A type of loop used for iteration

d) An operator for performing arithmetic calculations

Answer: b) A named storage location that can hold a value

2. Analysis: Which of the following is not a fundamental data type in C++?

a) int

b) float

c) string

d) bool

Answer: c) string

3. Creation/Synthesis: Which keyword is used to create a new object from a class in C++?

a) new

b) create

c) object

d) instance

Answer: a) new
4. Psychomotor: Which operator is used to access a member of an object in C++?

a) .

b) :

c) ->

d) ::

Answer: a) .

5. Understanding: What does the term "scope" refer to in C++?

a) The visibility and accessibility of variables, functions, and objects

b) The process of converting code into machine language

c) A type of loop used for conditional statements

d) The order in which statements are executed

Answer: a) The visibility and accessibility of variables, functions, and objects

6. Analysis: Which of the following loops is best suited for situations where the number of iterations is
known?

a) for loop

b) while loop

c) do-while loop

d) switch statement

Answer: a) for loop


7. Creation/Synthesis: Which header file should be included to use the input/output stream objects in C+
+?

a) <iostream>

b) <stdio.h>

c) <string>

d) <cmath>

Answer: a) <iostream>

8. Psychomotor: How do you declare a constant variable in C++?

a) const var_name;

b) constant var_name;

c) var_name const;

d) const data_type var_name;

Answer: d) const data_type var_name;

9. Understanding: What is the purpose of the return type in a function declaration in C++?

a) It specifies the number of arguments the function takes.

b) It indicates the name of the function.

c) It specifies the data type of the value returned by the function.

d) It determines the visibility of the function.

Answer: c) It specifies the data type of the value returned by the function.

10. Analysis: Which of the following is an example of a unary operator in C++?


a) +

b) +

c) *

d) %

Answer: a) +

11. Creation/Synthesis: Which of the following is a valid way to initialize an array in C++?

a) int arr[] = {1, 2, 3};

b) int arr[3];

c) int arr[3] = {1, 2, 3};

d) All of the above

Answer: d) All of the above

12. Psychomotor: Which operator is used to allocate memory dynamically in C++?

a) ::

b) *

c) &

d) =

Answer: b) *

13. Understanding: What does the "break" statement do in C++?

a) Terminates the current loop and transfers control to the next iteration
b)

Terminates the program execution

c) Exits a function and returns a value

d) Skips the current iteration of a loop

Answer: a) Terminates the current loop and transfers control to the next iteration

14. Analysis: What is the purpose of the "sizeof" operator in C++?

a) It returns the memory size of a data type or object

b) It performs division and returns the quotient

c) It checks if a condition is true or false

d) It concatenates two strings

Answer: a) It returns the memory size of a data type or object

15. Creation/Synthesis: Which of the following is not a valid way to pass arguments to a function in C++?

a) Pass by value

b) Pass by reference

c) Pass by pointer

d) Pass by class

Answer: d) Pass by class

16. Psychomotor: How do you open a file for writing in C++?


a) ifstream file("filename.txt");

b) fstream file("filename.txt");

c) ofstream file("filename.txt");

d) open("filename.txt", "w");

Answer: c) ofstream file("filename.txt");

17. Understanding: What is the purpose of the "endl" manipulator in C++?

a) It inserts a newline character into the output stream

b) It ends the execution of the program

c) It clears the contents of the output stream

d) It flushes the input buffer

Answer: a) It inserts a newline character into the output stream

18. Analysis: Which of the following is not a logical operator in C++?

a) &&

b) ||

c) !

d) %

Answer: d) %

19. Creation/Synthesis: How do you define a function outside of a class in C++?

a) void functionName() {}
b) functionName() {}

c) functionName(void) {}

d) void functionName(void) {}

Answer: d) void functionName(void) {}

20. Psychomotor: How do you access the command-line arguments passed to a C++ program?

a) argc

b) argv[]

c) argv

d) argc[]

Answer: b) argv[]

21. Understanding: Which of the following is not a valid way to create a C++ class?

a) class ClassName {};

b) struct ClassName {};

c) typedef struct {} ClassName;

d) typedef class {} ClassName;

Answer: d) typedef class {} ClassName;

22. Analysis: Which header file should be included to use mathematical functions like sqrt() and pow() in
C++?

a) <math.h>

b) <cmath>
c) <iostream>

d) <fstream>

Answer: b) <cmath>

23. Creation/Synthesis: How do you declare a pointer variable in C++?

a) pointerType var_name;

b) var_name pointerType;

c) pointerType *var_name;

d) *pointerType var_name;

Answer: c) pointerType *var_name;

24. Psychomotor: Which operator is used to increment the value of a variable by 1 in C++?

a) ++

b) --

c) +=

d) *=

Answer: a) ++

25. Understanding: What is the purpose of the "cin" object in C++?

a) It is used to read input from the user

b) It is used to write output to the console

c) It is used to perform arithmetic calculations


d) It is used to manipulate file streams

Answer

: a) It is used to read input from the user

26. Analysis: Which of the following is an example of a post-increment operator in C++?

a) ++

b) --

c) +=

d) *=

Answer: b) --

27. Creation/Synthesis: How do you define a constant member function in a C++ class?

a) void const functionName() {}

b) functionName() const {}

c) void functionName() const {}

d) const void functionName() {}

Answer: c) void functionName() const {}

28. Psychomotor: Which function is used to dynamically deallocate memory in C++?

a) delete

b) free
c) deallocate

d) clear

Answer: a) delete

29. Understanding: What is the purpose of the "else" statement in C++?

a) It specifies the condition for a loop to continue executing

b) It terminates the program execution

c) It specifies the alternative block of code to execute if the condition of an "if" statement is false

d) It performs logical AND operation between two conditions

Answer: c) It specifies the alternative block of code to execute if the condition of an "if" statement is
false

30. Analysis: Which of the following is not a type of function overloading in C++?

a) Operator overloading

b) Constructor overloading

c) Function name overloading

d) Method overloading

Answer: c) Function name overloading

Certainly! Here are 30 multiple-choice questions related to a program that utilizes structures, arrays,
functions, and pointers. Each question is followed by its answer:

1. Application: Which programming construct is used to group related data items into a single entity in
C++?
a) Arrays

b) Pointers

c) Structures

d) Functions

Answer: c) Structures

2. Evaluation: What is the purpose of an array in C++?

a) It is used to store and manipulate a collection of related data elements.

b) It is used to allocate memory dynamically.

c) It is used to pass arguments to functions.

d) It is used to define the return type of a function.

Answer: a) It is used to store and manipulate a collection of related data elements.

3. Creation/Synthesis: Which of the following correctly defines a structure in C++?

a) struct myStruct {}

b) class myStruct {}

c) typedef struct myStruct {}

d) typedef class myStruct {}

Answer: a) struct myStruct {}

4. Application: Which programming construct allows you to pass a structure as a parameter to a function
in C++?

a) Arrays
b) Pointers

c) Structures

d) Functions

Answer: c) Structures

5. Evaluation: What is the purpose of a function in C++?

a) It is used to store and manipulate a collection of related data elements.

b) It is used to allocate memory dynamically.

c) It is used to define the return type of a structure.

d) It is used to encapsulate a set of instructions to perform a specific task.

Answer: d) It is used to encapsulate a set of instructions to perform a specific task.

6. Creation/Synthesis: Which of the following is the correct way to declare a function prototype in C++?

a) returnType functionName(parameters);

b) functionName(parameters) returnType;

c) returnType functionName(parameters) {};

d) functionName(parameters);

Answer: a) returnType functionName(parameters);

7. Application: Which programming construct is used to store the address of a variable in C++?

a) Arrays

b) Pointers
c) Structures

d) Functions

Answer: b) Pointers

8. Evaluation: What is the purpose of a pointer in C++?

a) It is used to store and manipulate a collection of related data elements.

b) It is used to allocate memory dynamically.

c) It is used to access and modify the value stored at a specific memory address.

d) It is used to define the return type of a function.

Answer: c) It is used to access and modify the value stored at a specific memory address.

9. Creation/Synthesis: Which of the following correctly declares a pointer variable in C++?

a) int ptr;

b) ptr int;

c) int *ptr;

d) *int ptr;

Answer: c) int *ptr;

10. Application: How can you access the members of a structure variable in C++?

a) Using the dot operator (.)

b) Using the arrow operator (->)

c) Using the asterisk operator (*)


d) Using the double colon operator (::)

Answer: a) Using the dot operator (.)

11. Evaluation: What is the purpose of the arrow operator (->) in C++?

a) It is used to declare a pointer variable.

b) It is used to perform arithmetic calculations.

c) It is used to access the members of a structure through a pointer.

d) It is used to determine the size of

a data type.

Answer: c) It is used to access the members of a structure through a pointer.

12. Creation/Synthesis: Which of the following correctly defines a function that accepts a structure
pointer as a parameter in C++?

a) void functionName(struct myStruct *ptr) {}

b) void functionName(struct myStruct ptr) {}

c) void functionName(*struct myStruct ptr) {}

d) void functionName(struct *myStruct ptr) {}

Answer: a) void functionName(struct myStruct *ptr) {}

13. Application: How can you dynamically allocate memory for an array in C++?

a) Using the "new" keyword

b) Using the "malloc" function


c) Using the "calloc" function

d) Using the "allocate" keyword

Answer: a) Using the "new" keyword

14. Evaluation: What is the purpose of the "delete" keyword in C++?

a) It is used to deallocate dynamically allocated memory.

b) It is used to release resources held by a structure.

c) It is used to terminate the program execution.

d) It is used to define a destructor for a class.

Answer: a) It is used to deallocate dynamically allocated memory.

15. Creation/Synthesis: Which of the following correctly declares a dynamic array in C++ using pointers?

a) int *arr = {1, 2, 3};

b) int *arr = new int[3];

c) int arr[3];

d) int *arr;

Answer: b) int *arr = new int[3];

16. Application: How can you access an element in an array using a pointer in C++?

a) Using the dot operator (.)

b) Using the arrow operator (->)

c) Using the asterisk operator (*)


d) Using the subscript operator ([])

Answer: d) Using the subscript operator ([])

17. Evaluation: What is the purpose of the "sizeof" operator in C++?

a) It returns the memory size of a data type or object.

b) It performs arithmetic operations on operands.

c) It compares two values and returns a Boolean result.

d) It performs logical operations on operands.

Answer: a) It returns the memory size of a data type or object.

18. Creation/Synthesis: Which of the following correctly declares a structure variable in C++?

a) myStruct = new struct;

b) struct myStruct = {};

c) struct myStruct = new struct;

d) struct myStruct myVar;

Answer: d) struct myStruct myVar;

19. Application: How can you pass an array to a function in C++?

a) By passing the array size as a separate parameter.

b) By passing the array elements individually.

c) By passing the array as a pointer to its first element.

d) By passing the array using the dot operator (.)


Answer: c) By passing the array as a pointer to its first element.

20. Evaluation: What is the purpose of the "const" keyword in function parameters in C++?

a) It specifies that the function returns a constant value.

b) It specifies that the function cannot modify the parameter value.

c) It specifies that the function can modify the parameter value.

d) It specifies the number of parameters the function can accept.

Answer: b) It specifies that the function cannot modify the parameter value.

21. Creation/Synthesis: Which of the following is the correct way to declare a function that accepts an
array as a parameter in C++?

a) void functionName(int[] arr, int size

) {}

b) void functionName(int* arr, int size) {}

c) void functionName(int[] arr) {}

d) void functionName(int arr[], int size) {}

Answer: d) void functionName(int arr[], int size) {}

22. Application: How can you return a structure from a function in C++?

a) By using the return statement with the structure variable.

b) By passing the structure as a pointer to the function.

c) By using the "new" keyword to dynamically allocate the structure.


d) By using the "delete" keyword to deallocate the structure.

Answer: a) By using the return statement with the structure variable.

23. Evaluation: What is the purpose of the "static" keyword in C++?

a) It specifies that a variable is visible only within the current function.

b) It specifies that a variable retains its value across function calls.

c) It specifies that a variable is accessible from other translation units.

d) It specifies that a variable cannot be modified after its initialization.

Answer: b) It specifies that a variable retains its value across function calls.

24. Creation/Synthesis: Which of the following is the correct way to declare a static variable inside a
function in C++?

a) static int var;

b) int static var;

c) var static int;

d) int var;

Answer: a) static int var;

25. Application: How can you pass a structure by value to a function in C++?

a) By passing the structure using the dot operator (.)

b) By passing the structure as a pointer to the function

c) By passing the structure as a reference to the function

d) By passing the structure using the arrow operator (->)


Answer: c) By passing the structure as a reference to the function

26. Evaluation: What is the purpose of the "return" statement in a function in C++?

a) It terminates the program execution.

b) It returns a value from the function to the caller.

c) It assigns a value to a variable.

d) It performs a comparison between two values.

Answer: b) It returns a value from the function to the caller.

27. Creation/Synthesis: Which of the following correctly declares a pointer to a structure in C++?

a) struct *ptr;

b) struct ptr;

c) struct myStruct *ptr;

d) *struct myStruct ptr;

Answer: c) struct myStruct *ptr;

28. Application: How can you access the value stored at a memory address using a pointer in C++?

a) Using the dot operator (.)

b) Using the arrow operator (->)

c) Using the asterisk operator (*)

d) Using the subscript operator ([])


Answer: c) Using the asterisk operator (*)

29. Evaluation: What is the purpose of the "nullptr" keyword in C++?

a) It represents a null value for a pointer.

b) It represents an empty value for a structure.

c) It represents a default value for a function parameter.

d) It represents a placeholder value for a variable.

Answer: a) It represents a null value for a pointer.

30. Creation/Synthesis: Which of the following is the correct way to delete a dynamically allocated array
in C++?

a) delete arr;

b) delete[] arr;

c) free(arr);

d) clear(arr);

Answer: b) delete[] arr;

Certainly! Here are 30 multiple-choice questions related to I/O functionality for reading from and writing
to data and text files. Each question is followed by its answer:

1. Creation/Synthesis: Which header file is required to work with file I/O in C++?

a) \<iostream\>

b) \<fstream\>

c) \<cstdio\>

d) \<string\>
Answer: b) \<fstream\>

2. Application: Which mode should be used when opening a file to write data and discard the file's
current content?

a) std::ios::app

b) std::ios::trunc

c) std::ios::in

d) std::ios::out

Answer: b) std::ios::trunc

3. Evaluation: What is the purpose of the ifstream class in C++?

a) It is used for writing to files.

b) It is used for reading from files.

c) It is used for both reading from and writing to files.

d) It is used for managing file access modes.

Answer: b) It is used for reading from files.

4. Creation/Synthesis: Which of the following is the correct way to open a file named "data.txt" for
reading in C++?

a) ifstream file("data.txt", ios::out);

b) ifstream file("data.txt", ios::in);

c) ofstream file("data.txt", ios::in);

d) ofstream file("data.txt", ios::out);


Answer: b) ifstream file("data.txt", ios::in);

5. Application: How can you check if a file has been successfully opened in C++?

a) By using the is_open() function

b) By checking the fail() function

c) By checking the eof() function

d) By using the open() function

Answer: a) By using the is_open() function

6. Evaluation: Which function is used to read a single character from a file in C++?

a) getline()

b) get()

c) read()

d) put()

Answer: b) get()

7. Creation/Synthesis: Which of the following is the correct way to read a line of text from a file in C++?

a) getline(file, str);

b) file >> str;

c) read(file, str);

d) file.getline(str);
Answer: a) getline(file, str);

8. Application: How can you check if the end of a file has been reached in C++?

a) By using the is_open() function

b) By checking the fail() function

c) By checking the eof() function

d) By using the open() function

Answer: c) By checking the eof() function

9. Evaluation: What is the purpose of the ofstream class in C++?

a) It is used for writing to files.

b) It is used for reading from files.

c) It is used for both reading from and writing to files.

d) It is used for managing file access modes.

Answer: a) It is used for writing to files.

10. Creation/Synthesis: Which of the following is the correct way to open a file named "output.txt" for
writing in C++?

a) ofstream file("output.txt", ios::out);

b) ofstream file("output.txt", ios::in);

c) ifstream file("output.txt", ios::out);

d) ifstream file("output.txt", ios::in);

Answer: a) ofstream file("output.txt", ios::out);


11. Application

: How can you write a single character to a file in C++?

a) put()

b) read()

c) write()

d) get()

Answer: a) put()

12. Evaluation: Which function is used to write a string to a file in C++?

a) getline()

b) put()

c) read()

d) write()

Answer: d) write()

13. Creation/Synthesis: Which of the following is the correct way to check if a file write operation was
successful in C++?

a) By using the is_open() function

b) By checking the fail() function

c) By checking the eof() function

d) By using the open() function


Answer: b) By checking the fail() function

14. Application: How can you move the file pointer to a specific position within a file in C++?

a) seekp()

b) seekg()

c) seek()

d) movep()

Answer: b) seekg()

15. Evaluation: What is the purpose of the tellg() function in C++?

a) It returns the current position of the file pointer for reading.

b) It returns the current position of the file pointer for writing.

c) It returns the size of the file in bytes.

d) It returns the name of the file.

Answer: a) It returns the current position of the file pointer for reading.

16. Creation/Synthesis: Which of the following is the correct way to close a file in C++?

a) file.close();

b) close(file);

c) fclose(file);

d) file.end();

Answer: a) file.close();
17. Application: How can you check if a file exists before opening it in C++?

a) By using the exists() function

b) By checking the fail() function after opening it

c) By checking the eof() function after opening it

d) By using the open() function with the ios::exists flag

Answer: b) By checking the fail() function after opening it

18. Evaluation: What is the purpose of the ios::app mode when opening a file in C++?

a) It allows reading from the file.

b) It appends new data to the end of the file.

c) It truncates the file before opening.

d) It creates a new file if it doesn't exist.

Answer: b) It appends new data to the end of the file.

19. Creation/Synthesis: Which of the following is the correct way to read binary data from a file in C++?

a) read(file, buffer, size);

b) file.read(buffer, size);

c) getline(file, buffer);

d) get(file, buffer, size);

Answer: b) file.read(buffer, size);


20. Application: How can you write binary data to a file in C++?

a) write(file, buffer, size);

b) file.write(buffer, size);

c) put(file, buffer);

d) putline(file, buffer);

Answer: b) file.write(buffer, size);

21. Evaluation: What is the purpose of the ios::binary mode when opening a file in C++?

a) It allows reading from the file.

b) It appends new data to the end of the file.

c) It opens the file in text mode.

d) It opens the file in binary mode.

Answer: d) It opens the file in binary mode.

You might also like