0% found this document useful (0 votes)
13 views17 pages

TechXcel CPP Final Paper

The document contains a series of multiple-choice questions related to common errors and issues in C++ programming, covering topics such as syntax errors, function overloading, memory management, and threading. Each question presents a code snippet and asks the reader to identify the error or issue from the provided options. The questions are designed to test knowledge of C++ programming concepts and best practices.

Uploaded by

neverstudyhard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

TechXcel CPP Final Paper

The document contains a series of multiple-choice questions related to common errors and issues in C++ programming, covering topics such as syntax errors, function overloading, memory management, and threading. Each question presents a code snippet and asks the reader to identify the error or issue from the provided options. The questions are designed to test knowledge of C++ programming concepts and best practices.

Uploaded by

neverstudyhard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

TechXcel’25 Round 1(Bug Bounty Hunt)

Cpp

Q1. What is the error in the following code?


#include <iostream>
using namespace std;
int main() {
int a = 10
cout << "Value of a: " << a;
return 0;
}
A) Missing semicolon ; after int a = 10
B) cout is not defined
C) a should be a float
D) main() should return void

Q2. Find the error in this C++ code:


#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl
return 0;
}
Options:
A) endl is incorrect
B) cout should be replaced with printf
C) Missing semicolon ; after cout << "Hello" << endl
D) The code is correct

Q3. What is the issue in this program?


#include <iostream>
using namespace std;
int main() {
int arr[3] = {1, 2, 3};
cout << arr(1);
return 0;
}
Options:
A) arr is incorrectly
B) initialized arr(1) should be replaced with arr[1]
C) cout does not work with arrays
D) The code is correct

Q4. Identify the issue in the following code:


#include <iostream>
using namespace std;
void greet() {
cout << "Hello";
}
int main() {
greet;
return 0;
}
Options:
A) greet; should be greet();
B) cout should use printf
C) void greet() should return int
D) The program has no errors

Q5. Find the compile-time error in this program:


#include <iostream>
using namespace std;
int main() {
int x = 5;
int &y; // Reference must be initialized
y = x;
cout << y;
return 0;
}
Options:
A) No errors
B) y = x; is incorrect
C) cout << y; is invalid
D) Reference variable y must be initialized

Q6. What is the error in this C++ snippet?


#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a number: ";
cin >> x;
return "Done";
}
Options:
A) return "Done"; should return an int
B) cin does not work with integers
C) cout should be removed
D) x must be initialized

Q7. Identify the issue in this switch statement:


#include <iostream>
using namespace std;
int main() {
int num = 2;
switch(num) {
case 1:
cout << "One";
case 2:
cout << "Two";
default:
cout << "Default case";
}
return 0;
}
Options:
A) Missing break; statements inside cases
B) switch should take a character, not an integer
C) num should be a float
D) The program has no errors

Q8. What is wrong with this loop?


#include <iostream>
using namespace std;
int main() {
for(int i = 0; i < 5; i++);
{
cout << i << " ";
}
return 0;
}
Options:
A) i must be a global variable
B) cout should be inside the loop condition
C) Extra semicolon ; after for(int i = 0; i < 5; i++);
D) The loop will run infinitely
Q9. What is the issue in this array declaration?
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter size: ";
cin >> size;
int arr[size]; // Variable-length array
return 0;
}
Options:
A) size should be a const variable
B) C++ does not support variable-length arrays
C) arr[size] should be replaced with vector<int>
D) No errors

Q10. What is the problem in this program?


#include <iostream>
using namespace std;
int main() {
char ch = 'A';
cout << "Character: " << ch << endl;
ch++;
cout << "Next character: " << ch << endl;
return 1.5;
}
Options:
A) return 1.5; should return an int
B) ch cannot be incremented
C) cout does not support char output
D) No errors
Q11. What is the error in the following C++ code?
#include <iostream>
using namespace std;
void showMessage() {
cout << "Hello";
}

int main() {
showMessage;
return 0;
}
Options:
A) Function calls cannot be in main()
B) showMessage() should return int
C) cout should be replaced with printf
D) showMessage; should be showMessage();

Q12. Find the issue in the given code:


#include <iostream>
using namespace std;
int main() {
int x = 5;
int &y; // Reference must be initialized
y = x;
cout << y;
return 0;
}
Options:
A) No errors
B) y = x; is incorrect
C) cout << y; is invalid
D) Reference variable y must be initialized

Q13. Identify the error in this pointer program:


#include <iostream>
using namespace std;
int main() {
int *ptr;
*ptr = 10;
cout << *ptr;
return 0;
}
Options:
A) Pointer ptr is not initialized before dereferencing
B) ptr should be an integer
C) ptr should use malloc()
D) No errors

Q14. What is the issue in this file handling code?


#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file;
file.open("data.txt");
file << "Hello";
file.close();
file << "World"; // Writing after closing
return 0;
}
Options:
A) Writing to the file after closing it
B) open() should not be used
C) ofstream is not needed
D) No errors
Q15. What is wrong in this function declaration?
#include <iostream>
using namespace std;
void print(int x, int y = 5, int z);
int main() {
print(10, 20);
return 0;
}
Options:
A) z should also have a default value
B) Default parameters must be at the end
C) Function should return an int
D) No errors

Q16. What is the error in this program using function overloading?


#include <iostream>
using namespace std;
void display(int x) {
cout << "Integer: " << x;
}

void display(double x) {
cout << "Double: " << x;
}

int main() {
display(); // No argument passed
return 0;
}
Options:
A) display(double x) should use float x
B) display(); requires an argument
C) Overloading is not allowed in C++
D) The program has no errors

Q17. Find the problem in this function return statement:


#include <iostream>
using namespace std;
int add(int a, int b) {
int sum = a + b;
}

int main() {
cout << add(3, 5);
return 0;
}
Options:
A) Function add() does not return a value
B) sum should be a global variable
C) cout should be inside the function
D) No errors

Q18. What is the error in this class inheritance program?


#include <iostream>
using namespace std;
class Base {
int a;
};
class Derived : public Base {
int b;
};

int main() {
Derived obj;
cout << obj.a; // Accessing private member
return 0;
}
Options:
A) Derived must have a constructor
B) a should be protected
C) obj.a is private and cannot be accessed
D) The program has no errors

Q19. Identify the issue in this recursion-based program:


#include <iostream>
using namespace std;
void countDown(int n) {
cout << n << " ";
countDown(n - 1);
}

int main() {
countDown(5);
return 0;
}
Options:
A) should be in main()
B) Recursion is not supported in C++
C) Missing base condition in countDown() cout
D) No errors
Q20. What is the error in this template function?
#include <iostream>
using namespace std;
template <typename T>
void printValue(T value) {
cout << "Value: " << value << endl;
}
int main() {
printValue();
return 0;
}
Options:
A) Missing argument for printValue()
B) printValue() cannot be a template function
C) typename T should be replaced with class T
D) No errors

Q21. Identify the issue in this multithreading program:


#include <iostream>
#include <thread>
using namespace std;
void printMessage() {
cout << "Hello from thread!" << endl;
}
int main() {
thread t(printMessage);
t.join();
t.join(); // Joining thread twice
return 0;
}
Options:
A) thread is not a valid C++ class
B) A thread cannot be joined twice
C) t.join(); should be replaced with t.detach();
D) No errors

Q22. What is the issue in this memory management code?


#include <iostream>
using namespace std;
int main() {
int *ptr = new int(10);
delete ptr;
delete ptr; // Deleting memory twice
return 0;
}
Options:
A) No errors
B) delete ptr; should be replaced with free(ptr);
C) ptr should be a static variable
D) Double deletion of ptr causes undefined behavior

Q23. What is the error in this virtual function implementation?


#include <iostream>
using namespace std;
class Base {
public:
virtual void show() = 0; // Pure virtual function
};

class Derived : public Base {


public:
void display() { cout << "Derived class"; }
};

int main() {
Derived obj;
obj.show();
return 0;
}
Options:
A) Derived does not override show()
B) display() should be virtual
C) Base cannot have a pure virtual function
D) No errors

Q24. Identify the issue in this smart pointer usage:


#include <iostream>
#include <memory>
using namespace std;
int main() {
unique_ptr<int> p1(new int(10));
unique_ptr<int> p2 = p1; // Copying unique_ptr
cout << *p2;
return 0;
}
Options:
A) unique_ptr is not supported in C++`
B) p1 should be a shared_ptr
C) unique_ptr cannot be copied
D) No errors

Q25. What is the issue in this exception handling program?


#include <iostream>
using namespace std;
int main() {
try {
throw "Error!";
} catch (int e) { // Catching wrong type
cout << "Caught: " << e;
}
return 0;
}
Options:
A) Catch block should catch const char*
B) throw should use an integer
C) try should be inside a function
D) No errors

Q26. What is the issue in this move constructor implementation?


#include <iostream>
using namespace std;
class Demo {
public:
int *ptr;
Demo(int val) { ptr = new int(val); }
Demo(Demo &&obj) {
ptr = obj.ptr;
}
~Demo() { delete ptr; }
};

int main() {
Demo obj1(10);
Demo obj2 = move(obj1);
return 0;
}
Options:
A) No errors
B) ptr should be assigned using new in the move constructor
C) Move constructor should take a const & reference
D) Move constructor does not set obj.ptr to nullptr, leading to double deletion

Q27. Identify the error in this multiple inheritance program:


#include <iostream>
using namespace std;
class A {
public:
void show() { cout << "Class A"; }
};
class B {
public:
void show() { cout << "Class B"; }
};
class C : public A, public B {};
int main() {
C obj;
obj.show();
return 0;
}
Options:
A) C should use virtual inheritance
B) C should override show()
C) Ambiguous call to show()
D) No errors

Q28. What is the issue in this lambda function usage?


#include <iostream>
using namespace std;
int main() {
auto lambda = []() { static int x = 0; return ++x; };
cout << lambda() << " " << lambda();
return 0;
}
Options:
A) The lambda function should capture x by reference
B) The lambda function modifies a static variable, leading to shared state across calls
C) The lambda function cannot have static variables
D) No errors

Q29. Identify the issue in this std::vector usage:


#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> vec(5);
vec[6] = 10;
cout << vec[6];
return 0;
}
Options:
A) Out-of-bounds access on vec[6] causes undefined behavior
B) vec should be declared using vector<int> vec;
C) vec should be initialized with push_back()
D) No errors

Q30. What is the problem in this C++ 11 threading code?


#include <iostream>
#include <thread>
using namespace std;
void task() {
cout << "Task executed";
}
int main() {
thread t1(task);
if (t1.joinable()) {
t1.detach();
t1.join();
}
return 0;
}
Options:
A) No errors
B) joinable() should not be used
C) t1 should be a std::async instead
D) A detached thread cannot be joined

You might also like