0% found this document useful (0 votes)
15 views33 pages

Round 1 - Programming

Uploaded by

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

Round 1 - Programming

Uploaded by

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

- Write the output of the given code snippets or identify any errors.

- Answer all questions to the best of your ability.

1. What is the output of the following C code?

#include <stdio.h>

void func(int *ptr) {


*ptr = 10;
}

int main() {
int x = 5;
func(&x);
printf("%d\n", x);
return 0;
}

Output: ___________________

2. What is the output of the following C code?

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d\n", *(ptr + 2));
return 0;
}
```

Output: ___________________

3. Identify the error(s) in the following C code, if any:

#include <stdio.h>

int main() {
int a = 10;
if(a = 5) {
printf("a is 5\n");
} else {
printf("a is not 5\n");
}
return 0;
}

Output: ___________________

4. What is the output of the following Java code?

public class Test {


public static void main(String[] args) {
String str = "hello";
str = str + " world";
System.out.println(str);
}
}

Output: ___________________

5. What is the output of the following Java code?

public class Test {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for(int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 2;
}
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output: ___________________
6. Identify the error(s) in the following Java code, if any:

public class Test {


public static void main(String[] args) {
int x = 10;
int y = 0;
System.out.println(x / y);
}
}

Output: ___________________

7. What is the output of the following C++ code?

#include <iostream>
using namespace std;

void func(int &ref) {


ref = 20;
}

int main() {
int x = 10;
func(x);
cout << x << endl;
return 0;
}

Output: ___________________

8. What is the output of the following C++ code?

#include <iostream>
using namespace std;

class Test {
public:
static int count;
Test() {
count++;
}
};

int Test::count = 0;

int main() {
Test t1, t2, t3;
cout << Test::count << endl;
return 0;
}

Output: ___________________

**9. Identify the error(s) in the following C++ code, if any:**

```cpp
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test() {
x = 0;
}
void display() {
cout << x << endl;
}
};

int main() {
Test t;
t.display();
return 0;
}

Output: ___________________
**1. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int a = 5, b = 10, c;
c = a + b;
printf("%d\n", c);
return 0;
}
```

Output: ___________________

**2. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int x = 10;
x = x++ + ++x;
printf("%d\n", x);
return 0;
}
```

Output: ___________________

**3. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d\n", *(p + 3));
return 0;
}
```

Output: ___________________
**4. What is the output of the following C code?**

```c
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
```

Output: ___________________

**5. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
char str[] = "hello";
printf("%c\n", *(str + 2));
return 0;
}
```

Output: ___________________

**6. Identify the error(s) in the following C code, if any:**

```c
#include <stdio.h>

int main() {
int *p = NULL;
*p = 10;
printf("%d\n", *p);
return 0;
}
```

Error: ___________________

**7. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int a = 5, b = 10;
if (a < b) {
printf("a is less than b\n");
} else {
printf("a is not less than b\n");
}
return 0;
}
```

Output: ___________________

**8. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
return 0;
}
```

Output: ___________________

**9. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
return 0;
}
```

Output: ___________________

**10. Identify the error(s) in the following C code, if any:**

```c
#include <stdio.h>

int main() {
int a = 10;
int b = 0;
int c = a / b;
printf("%d\n", c);
return 0;
}
```

Error: ___________________

**11. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int x = 5;
int *p = &x;
*p = 20;
printf("%d\n", x);
return 0;
}
```
Output: ___________________

**12. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
char ch = 'A';
printf("%c\n", ch + 1);
return 0;
}
```

Output: ___________________

**13. Identify the error(s) in the following C code, if any:**

```c
#include <stdio.h>

int main() {
int x = 5;
int y = 10;
if (x = y) {
printf("x is equal to y\n");
} else {
printf("x is not equal to y\n");
}
return 0;
}
```

Error: ___________________

**14. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int x = 5;
if (x > 2) {
x = x + 10;
}
printf("%d\n", x);
return 0;
}
```

Output: ___________________

**15. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int a = 5, b = 3;
int c = a & b;
printf("%d\n", c);
return 0;
}
```

Output: ___________________

**16. Identify the error(s) in the following C code, if any:**

```c
#include <stdio.h>

int main() {
int arr[5];
printf("%d\n", arr[5]);
return 0;
}
```

Error: ___________________

**17. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i * i);
}
return 0;
}
```

Output: ___________________

**18. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int a = 5, b = 10;
printf("%d\n", a > b ? a : b);
return 0;
}
```

Output: ___________________

**19. What is the output of the following C code?**

```c
#include <stdio.h>

int main() {
int arr[3] = {1, 2, 3};
int *p = arr + 1;
printf("%d\n", *p);
return 0;
}
```

Output: ___________________

**20. Identify the error(s) in the following C code, if any:**

```c
#include <stdio.h>

int main() {
int a = 5;
if (a > 2)
printf("a is greater than 2\n");
printf("This will always print\n");
else
printf("This will never print\n");
return 0;
}
```

Error: ___________________

---

### Java Section

**21. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println(a + b);
}
}
```

Output: ___________________

**22. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int x = 5;
if (x > 2) {
x += 10;
}
System.out.println(x);
}
}
```
Output: ___________________

**23. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
String str = "hello";
str = str.toUpperCase();
System.out.println(str);
}
}
```

Output: ___________________

**24. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
for (int i : arr) {
System.out.print(i + " ");
}
}
}
```

Output: ___________________

**25. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int x = 5;
System.out.println(x == 5 ? "True" : "False");
}
}
```

Output: ___________________
**26. Identify the error(s) in the following Java code, if any:**

```java
public class Test {
public static void main(String[] args) {
int[] arr = new int[3];
System.out.println(arr[3]);
}
}
```

Error: ___________________

**27. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = x + y;
System.out.println(z);
}
}
```

Output: ___________________

**28. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = x > y ? x : y;
System.out.println(z);
}
}
``

Output: ___________________
**29. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int[] arr = {10, 20, 30};
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] / 2;
}
for (int i : arr) {
System.out.print(i + " ");
}
}
}
```

Output: ___________________

**30. Identify the error(s) in the following Java code, if any:**

```java
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 0;
int z = x / y;
System.out.println(z);
}
}
```

Error: ___________________

**31. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
String str = "123";
int num = Integer.parseInt(str);
System.out.println(num);
}
}
```

Output: ___________________

**32. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2);
}
}
```

Output: ___________________

**33. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1 == str2);
}
}
```

Output: ___________________

**34. Identify the error(s) in the following Java code, if any:**

```java
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}
}
}
```
Error: ___________________

**35. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.print(i * i + " ");
}
}
}
```

Output: ___________________

**36. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int a = 5;
int b = 10;
int c = a & b;
System.out.println(c);
}
}
```

Output: ___________________

**37. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println(a | b);
}
}
```
Output: ___________________

**38. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int x = 5;
x = x++ + ++x;
System.out.println(x);
}
}
```

Output: ___________________

**39. Identify the error(s) in the following Java code, if any:**

```java
public class Test {
public static void main(String[] args) {
int x = 5;
if (x = 10) {
System.out.println("x is 10");
} else {
System.out.println("x is not 10");
}
}
}
```

Error: ___________________

**40. What is the output of the following Java code?**

```java
public class Test {
public static void main(String[] args) {
int x = 5;
while (x < 10) {
x++;
}
System.out.println(x);
}
}
```

Output: ___________________

---

### C++ Section

**41. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int a = 5, b = 10;
cout << a + b << endl;
return 0;
}
```

Output: ___________________

**42. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int x = 5;
if (x > 2) {
x += 10;
}
cout << x << endl;
return 0;
}
```

Output: ___________________

**43. What is the output of the following C++ code?**


```cpp
#include <iostream>
using namespace std;

void func(int &ref) {


ref = 20;
}

int main() {
int x = 10;
func(x);
cout << x << endl;
return 0;
}
```

Output: ___________________

**44. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int arr[3] = {1, 2, 3};
for (int i = 0; i < 3; i++) {
cout << arr[i] << " ";
}
return 0;
}
```

Output: ___________________

**45. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int a = 5, b = 3;
int c = a & b;
cout << c << endl;
return 0;
}
```

Output: ___________________

**46. Identify the error(s) in the following C++ code, if any:**

```cpp
#include <iostream>
using namespace std;

int main() {
int arr[3] = {1, 2, 3};
cout << arr[3] << endl;
return 0;
}
```

Error: ___________________

**47. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Test {
public:
static int count;
Test() {
count++;
}
};

int Test::count = 0;

int main() {
Test t1, t2, t3;
cout << Test::count << endl;
return 0;
}
```
Output: ___________________

**48. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

void swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}

int main() {
int x = 5, y = 10;
swap(x, y);
cout << x << " " << y << endl;
return 0;
}
```

Output: ___________________

**49. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int x = 5;
cout << (x > 2 ? x : 2) << endl;
return 0;
}
```

Output: ___________________

**50. Identify the error(s) in the following C++ code, if any:**

```cpp
#include <iostream>
using namespace std;

int main() {
int x = 5;
if (x = 10) {
cout << "x is 10" << endl;
} else {
cout << "x is not 10" << endl;
}
return 0;
}
```

Error: ___________________

**51. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int x = 10;
cout << --x << endl;
return 0;
}
```

Output: ___________________

**52. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Test {
public:
int a;
Test() : a(10) {}
};

int main() {
Test t;
cout << t.a << endl;
return 0;
}
```

Output: ___________________

**53. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int a) : x(a) {}
int getX() { return x; }
};

int main() {
Test t(5);
cout << t.getX() << endl;
return 0;
}
```

Output: ___________________

**54. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

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

class Derived : public Base {


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

int main() {
Derived d;
d.display();
return 0;
}
```

Output: ___________________

**55. Identify the error(s) in the following C++ code, if any:**

```cpp
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test() : x(10) {}
void show() {
cout << x << endl;

}
};

int main() {
Test t;
cout << t.x << endl;
return 0;
}
```

Error: ___________________

**56. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Test {
public:
static int x;
};

int Test::x = 5;

int main() {
Test t;
cout << t.x << endl;
return 0;
}
```

Output: ___________________

**57. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Base {
public:
virtual void display() {
cout << "Base class" << endl;
}
};

class Derived : public Base {


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

int main() {
Base* b = new Derived();
b->display();
return 0;
}
```
Output: ___________________

**58. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Test {
public:
Test() {
cout << "Constructor" << endl;
}
~Test() {
cout << "Destructor" << endl;
}
};

int main() {
Test t;
return 0;
}
```

Output: ___________________

**59. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int x = 10;
int y = x++;
cout << y << endl;
return 0;
}
```

Output: ___________________

**60. Identify the error(s) in the following C++ code, if any:**


```cpp
#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int a) : x(a) {}
void show() const {
x = 20;
cout << x << endl;
}
};

int main() {
Test t(10);
t.show();
return 0;
}
```

Error: ___________________

**61. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Test {
public:
int a;
Test(int x) : a(x) {}
};

int main() {
Test t1(10);
Test t2 = t1;
cout << t2.a << endl;
return 0;
}
```
Output: ___________________

**62. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Base {
public:
Base() {
cout << "Base Constructor" << endl;
}
~Base() {
cout << "Base Destructor" << endl;
}
};

class Derived : public Base {


public:
Derived() {
cout << "Derived Constructor" << endl;
}
~Derived() {
cout << "Derived Destructor" << endl;
}
};

int main() {
Derived d;
return 0;
}
```

Output: ___________________

**63. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

void func() {
static int x = 0;
x++;
cout << x << endl;
}

int main() {
func();
func();
return 0;
}
```

Output: ___________________

**64. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

template <typename T>


T add(T a, T b) {
return a + b;
}

int main() {
cout << add(5, 10) << endl;
cout << add(2.5, 3.5) << endl;
return 0;
}
```

Output: ___________________

**65. Identify the error(s) in the following C++ code, if any:**

```cpp
#include <iostream>
using namespace std;

int main() {
int x = 5;
int& y = x;
y = 10;
cout << x << endl;
return 0;
}
```

Error: ___________________

**66. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int x = 5;
cout << &x << endl;
return 0;
}
```

Output: ___________________

**67. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

class Test {
public:
Test() {
cout << "Constructor" << endl;
}
~Test() {
cout << "Destructor" << endl;
}
};

void func() {
Test t;
}

int main() {
func();
return 0;
}
```

Output: ___________________

**68. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 10;
swap(x, y);
cout << x << " " << y << endl;
return 0;
}
```

Output: ___________________

**69. What is the output of the following C++ code?**

```cpp
#include <iostream>
using namespace std;

int main() {
int arr[3] = {1, 2, 3};
for (int i = 0; i < 3; i++) {
arr[i] = arr[i] * 2;
}
for (int i = 0; i < 3; i++) {
cout << arr[i] << " ";
}
return 0;
}
```

Output: ___________________

70. Identify the error(s) in the following C++ code, if any:


```cpp
#include <iostream>
using namespace std;

class Test {
public:
int x;
Test() : x(10) {}
};

int main() {
Test t;
cout << t.x << endl;
return 0;
}
```

Error: ___________________

You might also like