Functions Questions
Practice Functions MCQs with answers and explanations. Page 3 of 5.
Category
C++ Programming
Topic
Functions
Page
3 / 5
Mode
Practice
Questions
Open any question to view the answer and explanation.
C syntax rule: “A function cannot be defined inside another function.” Decide whether this statement is correct or incorrect (standard ISO C, not extensions).
Open
View answer
Modern C (C99 and later): “If the return type for a function is not specified, it defaults to int.” Decide whether this statement is correct or incorrect.
Open
View answer
C control flow: “A function may contain multiple return statements that return different values along different paths.” Decide whether this statement is correct or incorrect (assume a non-void function).
Open
View answer
Parameter passing in C: “Functions can be called either by value or by reference.” Decide whether this statement is correct or incorrect.
Open
View answer
Linking and symbol visibility: “Names of functions in two different source files linked into one program must be unique.” Decide whether this statement is correct or incorrect.
Open
View answer
Compiler diagnostics: “If a function contains two return statements one after another, the compiler will generate warnings.” Decide Yes or No (consider typical conforming compilers).
Open
View answer
C return types: “Functions cannot return a floating-point number.” Decide Yes or No.
Open
View answer
In C programming, is there a fixed maximum number of arguments a function can accept (for example, “at most 12”)?
Provide the best general answer for standard-conforming C across common platforms.
Open
View answer
In C, must every function return a value, or are non-returning (void) functions valid?
Answer for standard C across common platforms.
Open
View answer
Modern ISO C (C99/C11+): will the following functions compile and work without a prior prototype for f2?
int f1(int a, int b)
{
return ( f2(20) );
}
int f2(int a)
{
return (a*a);
}
Assume no forward declaration is provided.
Open
View answer
In C, may a function legally contain multiple return statements, or should there never be more than one?
Open
View answer
Algorithmic performance in C: as a general rule, does recursion usually run slower than equivalent loops due to call overhead and stack usage?
Open
View answer
Recursion and system resources: can too many recursive calls in a C program cause a stack overflow on typical systems?
Open
View answer
C++ name clashes and multiple inheritance: will this program compile, and what does the qualified call print?
#include<iostream.h>
static int Result;
class India{ public: void Change(int x=10,int y=20,int z=30){ cout << x+y+z; } void Display(int x=40, float y=50.00){ Result = x % x; cout << Result; } };
class CuriousTab{ int x,y; public: void Change(int x,int y=50){ cout << x+y; } };
class CuriousTab: public India, public CuriousTab{ public: void Display(int x=10,int xx=100,int xxx=1000){ Result = x + xx % x * x; cout << Result; } };
int main(){ CuriousTab objCuriousTab; objCuriousTab.India::Display(10, 20.00); }
Open
View answer
C++ defaults and pre-decrement with mixed integer/float args: evaluate the expression a % 20 + c * --b for the provided call.
#include<iostream.h>
class CuriousTabSample{
public:
int a; float b;
void CuriousTabFunction(int a, float b, float c=100.0f){ cout << a % 20 + c * --b; }
};
int main(){ CuriousTabSample obj; obj.CuriousTabFunction(20, 2.000000f, 5.0f); }
Open
View answer
C++ swapping via pointers to members in two different objects: after Exchange(&objA.x, &objB.y), what prints?
#include<iostream.h>
class CuriousTab{
public:
int x, y;
CuriousTab(int xx=10,int yy=20){ x=xx; y=yy; }
void Exchange(int*, int*);
};
void CuriousTab::Exchange(int *x, int *y){ int t; t=*x; *x=*y; *y=t; }
int main(){ CuriousTab objA(30,40), objB(50); objA.Exchange(&objA.x, &objB.y); cout << objA.x << " " << objB.y << endl; }
Open
View answer
C++ pointer size vs. string contents: what does this program actually print when taking sizeof on a char* variable (legacy headers)?
#include<iostream.h>
#include<string.h>
#include<malloc.h>
class CuriousTabString{
char txtName[20];
public:
CuriousTabString(char txtTemp=NULL){ if(txtTemp!=NULL) strcpy(txtName, txtTemp); }
void Display(){ cout << txtName; }
};
int main(){ char txtName=(char)malloc(10); strcpy(txtName, "CuriousTab"); txtName = 48; // overwrite first char
CuriousTabString objTemp(txtName); cout << sizeof(txtName); }
Open
View answer
C++ references and default constructor print: what will this program output when passing pre-incremented and pre-decremented references to the constructor?
#include<iostream.h>
class CuriousTab
{
int x, y, z;
public:
CuriousTab(int x = 100, int y = 30, int z = 0)
{
this->x = x;
this->y = y;
this->z = z;
Display();
}
void Display()
{
cout << x << " " << y << " " << z;
}
};
int main()
{
int a = 0, b = 1, c = 2;
int &x = ++a; // a becomes 1, x binds to a
int &y = --b; // b becomes 0, y binds to b
int z = c + b - -c; // z = c + b + c = 4
CuriousTab objCuriousTab(x, y, z);
return 0;
}
Open
View answer
C++ recursion with a static accumulator and member mutation: what does this program print?
#include<iostream.h>
class TestDrive
{
int x;
public:
TestDrive(int xx) { x = xx; }
int DriveIt(void);
};
int TestDrive::DriveIt(void)
{
static int value = 0; // accumulates across recursion
int m;
m = x % 2; // current parity bit
x = x / 2; // shift right by 1 (integer divide)
if ((x / 2)) DriveIt();
value = value + m * 10;
return value;
}
int main()
{
TestDrive TD(1234);
cout << TD.DriveIt() * 10 << endl;
return 0;
}
Open
View answer
C++ default arguments and integer truncation: what is printed by this program?
#include<iostream.h>
int main()
{
float Amount;
float Calculate(float P = 5.0, int N = 2, float R = 2.0);
Amount = Calculate();
cout << Amount << endl;
return 0;
}
float Calculate(float P, int N, float R)
{
int Year = 1;
float Sum = 1;
Sum = Sum * (1 + P * ++N * R);
Year = (int)(Year + Sum);
return Year;
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.