Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Functions Questions
C++ pointer size vs. string contents: what does this program actually print when taking sizeof on a char* variable (legacy headers)? #include
#include
#include
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); }
C++ references and default constructor print: what will this program output when passing pre-incremented and pre-decremented references to the constructor? #include
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; }
C++ recursion with a static accumulator and member mutation: what does this program print? #include
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; }
C++ default arguments and integer truncation: what is printed by this program? #include
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; }
C++ loop with pre-incremented counter and default parameters: what value does PowerFinder::Power print for (2, 6)? #include
class PowerFinder { public: void Power(int x = 1, int y = 1) { int P = 1, i = 1; while (++i <= y) { P *= x; } cout << P << endl; } }; int main() { PowerFinder FP; FP.Power(2, 6); return 0; }
C++ default parameters and base-class constructor: what does this program print when calling CountIt with two arguments? #include
class BaseCounter { protected: long int count; public: void CountIt(int x, int y = 10, int z = 20) { count = 0; cout << x << " " << y << " " << z << endl; } BaseCounter() { count = 0; } BaseCounter(int x) { count = x; } }; class DerivedCounter : public BaseCounter { public: DerivedCounter() { } DerivedCounter(int x) : BaseCounter(x) { } }; int main() { DerivedCounter objDC(30); objDC.CountIt(40, 50); return 0; }
C++ default argument as null pointer to a 2D array parameter and uninitialized storage: what will this program display? #include
class CuriousTabArray { int array[3][3]; public: CuriousTabArray(int arr[3][3] = NULL) { if (arr != NULL) for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) array[i][j] = i + j; } void Display(void) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cout << array[i][j] << " "; } }; int main() { CuriousTabArray objBA; // uses default argument NULL objBA.Display(); return 0; }
C++ default parameter override: what exact output is produced by this call? #include
void MyFunction(int a, int b = 40) { cout << " a = " << a << " b = " << b << endl; } int main() { MyFunction(20, 30); return 0; }
C++ function pointer to a function with defaults: what does Note(30) print when it calls Look(x) with a missing second argument? #include
typedef void(*FunPtr)(int); int Look(int = 10, int = 20); void Note(int); int main() { FunPtr ptr = Note; (*ptr)(30); return 0; } int Look(int x, int y) { return (x + y % 20); } void Note(int x) { cout << Look(x) << endl; // calls Look(x, 20) }
C++ overload resolution on integral and floating return types: which overload is called and what is printed? #include
long GetNumber(long int Number) { return --Number; } float GetNumber(int Number) { return ++Number; } int main() { int x = 20; int y = 30; cout << GetNumber(x) << " "; cout << GetNumber(y); return 0; }
C++ evaluation order with pre/post operators and defaults: what does this call print? #include
int CuriousTabFunction(int a, int b = 3, int c = 3) { cout << ++a * ++b * --c; return 0; } int main() { CuriousTabFunction(5, 0, 0); return 0; }
In C++ (recursion with a static index over a C-string), what does the following program print when searching for the character 't' in the message "Welcome to CuriousTab.com!"? #include
#include
class CuriousTab { char txtMsg[50]; public: CuriousTab(char *str = NULL) { if(str != NULL) strcpy(txtMsg, str); } int CuriousTabFunction(char ch); }; int CuriousTab::CuriousTabFunction(char ch) { static int i = 0; if(txtMsg[i++] == ch) return strlen((txtMsg + i)) - i; else return CuriousTabFunction(ch); } int main() { CuriousTab objCuriousTab("Welcome to CuriousTab.com!"); cout << objCuriousTab.CuriousTabFunction('t'); return 0; }
In C++ (assignment in a conditional), analyze the following program and determine what is printed by Display(). Note the deliberate use of "if (l = 0)" vs "if (l == 0)". #include
class AreaFinder { float l, b, h; float result; public: AreaFinder(float hh = 0, float ll = 0, float bb = 0) { l = ll; b = bb; h = hh; } void Display(int ll) { if(l = 0) result = 3.14f * h * h; else result = l * b; cout << result; } }; int main() { AreaFinder objAF(10, 10, 20); objAF.Display(0); return 0; }
In C++ (default arguments and pre-increment effects), evaluate the function call and compute the exact integer result. #include
long CuriousTabFunction(int x, int y = 5, float z = 5) { return (++x * ++y + (int)++z); } int main() { cout << CuriousTabFunction(20, 10); return 0; }
In C++ (overload resolution with default parameters), what does the following program print? Focus on which Addition overload is selected in each call. #include
struct MyData { public: int Addition(int a, int b = 10) { return (a *= b + 2); } float Addition(int a, float b); }; int main() { MyData data; cout << data.Addition(1) << " "; cout << data.Addition(3, 4); return 0; }
In C++ (const parameters and integer arithmetic), determine the output of the following code. Consider integer operator precedence and the final multiplication by 0.2. #include
const double CuriousTabConstant(const int, const int = 0); int main() { const int c = 2; cout << CuriousTabConstant(c, 10) << " "; cout << CuriousTabConstant(c, 20) << endl; return 0; } const double CuriousTabConstant(const int x, const int y) { return ( (y + (y * x) * x % y) * 0.2 ); }
In C++ (constructor selection with defaults and a derived-class initializer), what does this program print? Pay attention to which Base constructor is called and the values assigned. #include
class Base { public: int S, A, M; Base(int x, int y) { S = y - y; A = x + x; M = x * x; } Base(int, int y = 'A', int z = 'B') { S = y; A = y + 1 - 1; M = z - 1; } void Display(void) { cout << S << " " << A << " " << M << endl; } }; class Derived : public Base { int x, y, z; public: Derived(int xx = 65, int yy = 66, int zz = 67) : Base(x) { x = xx; y = yy; z = zz; } void Display(int n) { if(n) Base::Display(); else cout << x << " " << y << " " << z << endl; } }; int main() { Derived objDev; objDev.Display(-1); return 0; }
In C++ (default arguments and implicit conversions), what does this nested class call print? Pay attention to how the char literal 'b' matches the float defaulted parameter. #include
struct MyStructure { class MyClass { public: void Display(int x, float y = 97.50, char ch = 'a') { cout << x << " " << y << " " << ch; } } Cls; } Struc; int main() { Struc.Cls.Display(12, 'b'); return 0; }
In C++ (default pointer argument bound to a static global), what is printed? Note that a local variable named b shadows the global, but the call passes both pointers explicitly. #include
static int b = 0; void DisplayData(int *x, int *y = &b) { cout << *x << " " << *y; } int main() { int a = 10, b = 20; DisplayData(&a, &b); return 0; }
In C++ (recursive digit processing with a static accumulator), what number is printed for the initial value 12345? #include
class CuriousTab { int Num; public: CuriousTab(int x) { Num = x; } int CuriousTabFunction(void); }; int CuriousTab::CuriousTabFunction(void) { static int Sum = 0; int Dec; Dec = Num % 10; Num = Num / 10; if((Num / 100)) CuriousTabFunction(); Sum = Sum * 10 + Dec; return Sum; } int main() { CuriousTab objCuriousTab(12345); cout << objCuriousTab.CuriousTabFunction(); return 0; }
1
2
3
4