Computer Programming
NEC Electrical and Electronic Engineering · 592 practice MCQs
The 'Computer Programming' chapter in the Electrical and Electronic Engineering discipline of the NEC licensing exam is crucial for understanding fundamental programming concepts and their applications in engineering. This chapter covers key topics such as pointers, memory management, and object-oriented programming principles.
With 592 practice MCQs, this chapter equips you with the knowledge needed to tackle programming-related questions effectively. To prepare, focus on mastering pointer operations, memory allocation, and the principles of encapsulation and polymorphism, which are essential for the exam.
Sample questions with answers
1.What can pointers be used to create?
- A.Only global variables
- B.Only arrays
- C.None
- D.Linked lists, trees, dynamic structures
Why: Pointers are used to create dynamic data structures like linked lists, trees, and other dynamic structures because they allow for flexible memory allocation and manipulation.
2.What is true about a pointer to a 2D array?
- A.It points to the last element
- B.It cannot point to anything
- C.It points to the first element
- D.It points to the first row
Why: A pointer to a 2D array points to the first row because it represents the starting address of the array in memory, allowing access to all elements in the array through indexing.
3.What is true regarding dangling pointers in a 2D dynamic array?
- A.Happens if a row is freed but pointer is still in use
- B.Pointer automatically becomes null
- C.Pointer resets to zero by itself
- D.Does not happen
Why: Dangling pointers occur when a row of a dynamic 2D array is freed while the pointer still references it, leading to undefined behavior if the pointer is used afterward.
4.What does encapsulation enhance?
- A.Only stack usage
- B.Only memory allocation
- C.Security of data and modularity
- D.Speed of execution
Why: Encapsulation enhances security of data and modularity by restricting access to certain components of an object, allowing for controlled interaction and reducing dependencies.
5.How do you declare a pointer to a 3x4 multidimensional array?
- A.int (*p)[4];
- B.int **p;
- C.int *p[4];
- D.int p*[];
Why: To declare a pointer to a 3x4 multidimensional array, the correct syntax is int (*p)[4]; which indicates that p points to an array of 4 integers.
6.When does a virtual function get resolved?
- A.During compilation
- B.At linking stage
- C.Cannot be resolved
- D.During execution
Why: A virtual function gets resolved during execution because it relies on the actual object type at runtime to determine which function implementation to call.
7.How do you dynamically allocate a structure that includes a string member?
- A.Only global strings
- B.One malloc for both
- C.malloc for the structure, then malloc for the string
- D.String cannot be dynamically allocated
Why: To dynamically allocate a structure with a string member, you need to allocate memory for both the structure and the string separately to ensure proper memory management.
8.What is the correct method to release dynamically allocated memory?
- A.clear(ptr);
- B.delete ptr;
- C.free(ptr);
- D.ptr = NULL;
Why: The correct method to release dynamically allocated memory in C is using free(ptr); which deallocates the memory previously allocated by malloc or similar functions.
9.What occurs if a recursive function lacks a base case?
- A.Error from the compiler
- B.Program executes as usual
- C.Stack overflow happens
- D.Function runs only once
Why: If a recursive function lacks a base case, it will continue to call itself indefinitely, leading to a stack overflow as the call stack exceeds its limit.
10.What is correct regarding overloading member functions?
- A.Can be overloaded by changing arguments
- B.Can be overloaded by just changing return type
- C.Overloading is based on access level
- D.Cannot be overloaded within a class
Why: Overloading member functions is possible by changing the arguments, as the compiler distinguishes between functions based on their parameter lists.
11.What is used to define a member function outside its class?
- A.Pointer operator ->
- B.Dot operator .
- C.Scope resolution operator ::
- D.Regular syntax
Why: The scope resolution operator :: is used to define a member function outside its class, allowing access to the class's members and methods in a clear manner.
12.What is the base case in a recursive function?
- A.Memory allocation
- B.Mathematical operation
- C.Limit of stack depth
- D.Stopping condition
Why: The base case in a recursive function is the stopping condition that prevents infinite recursion and allows the function to terminate properly.
Practice all 592 Computer Programming questions
Adaptive mock tests, AI explanations on every question, and a personalized study plan.
Frequently asked questions
- What can pointers be used to create?
- Pointers can be used to create dynamic data structures like linked lists, trees, and arrays.
- What is true about a pointer to a 2D array?
- A pointer to a 2D array points to the first element of the array, allowing access to its elements using pointer arithmetic.
- What is true regarding dangling pointers in a 2D dynamic array?
- Dangling pointers occur when a pointer references memory that has been freed, leading to undefined behavior.
- How do you declare a pointer to a 3x4 multidimensional array?
- You declare it as 'int (*ptr)[4];' where 'ptr' points to an array of 4 integers.
- When does a virtual function get resolved?
- A virtual function gets resolved at runtime based on the type of the object pointed to, enabling polymorphism.