Round 1 - Programming
Round 1 - Programming
#include <stdio.h>
int main() {
int x = 5;
func(&x);
printf("%d\n", x);
return 0;
}
Output: ___________________
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d\n", *(ptr + 2));
return 0;
}
```
Output: ___________________
#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: ___________________
Output: ___________________
Output: ___________________
6. Identify the error(s) in the following Java code, if any:
Output: ___________________
#include <iostream>
using namespace std;
int main() {
int x = 10;
func(x);
cout << x << endl;
return 0;
}
Output: ___________________
#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: ___________________
```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: ___________________
```c
#include <stdio.h>
int main() {
int x = 10;
x = x++ + ++x;
printf("%d\n", x);
return 0;
}
```
Output: ___________________
```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>
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
char str[] = "hello";
printf("%c\n", *(str + 2));
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
int *p = NULL;
*p = 10;
printf("%d\n", *p);
return 0;
}
```
Error: ___________________
```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: ___________________
```c
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
int a = 10;
int b = 0;
int c = a / b;
printf("%d\n", c);
return 0;
}
```
Error: ___________________
```c
#include <stdio.h>
int main() {
int x = 5;
int *p = &x;
*p = 20;
printf("%d\n", x);
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
char ch = 'A';
printf("%c\n", ch + 1);
return 0;
}
```
Output: ___________________
```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: ___________________
```c
#include <stdio.h>
int main() {
int x = 5;
if (x > 2) {
x = x + 10;
}
printf("%d\n", x);
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
int a = 5, b = 3;
int c = a & b;
printf("%d\n", c);
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
int arr[5];
printf("%d\n", arr[5]);
return 0;
}
```
Error: ___________________
```c
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i * i);
}
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("%d\n", a > b ? a : b);
return 0;
}
```
Output: ___________________
```c
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
int *p = arr + 1;
printf("%d\n", *p);
return 0;
}
```
Output: ___________________
```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
public class Test {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println(a + b);
}
}
```
Output: ___________________
```java
public class Test {
public static void main(String[] args) {
int x = 5;
if (x > 2) {
x += 10;
}
System.out.println(x);
}
}
```
Output: ___________________
```java
public class Test {
public static void main(String[] args) {
String str = "hello";
str = str.toUpperCase();
System.out.println(str);
}
}
```
Output: ___________________
```java
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
for (int i : arr) {
System.out.print(i + " ");
}
}
}
```
Output: ___________________
```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: ___________________
```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: ___________________
```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: ___________________
```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: ___________________
```java
public class Test {
public static void main(String[] args) {
String str = "123";
int num = Integer.parseInt(str);
System.out.println(num);
}
}
```
Output: ___________________
```java
public class Test {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2);
}
}
```
Output: ___________________
```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: ___________________
```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: ___________________
```java
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.print(i * i + " ");
}
}
}
```
Output: ___________________
```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: ___________________
```java
public class Test {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println(a | b);
}
}
```
Output: ___________________
```java
public class Test {
public static void main(String[] args) {
int x = 5;
x = x++ + ++x;
System.out.println(x);
}
}
```
Output: ___________________
```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: ___________________
```java
public class Test {
public static void main(String[] args) {
int x = 5;
while (x < 10) {
x++;
}
System.out.println(x);
}
}
```
Output: ___________________
---
```cpp
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
cout << a + b << endl;
return 0;
}
```
Output: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
if (x > 2) {
x += 10;
}
cout << x << endl;
return 0;
}
```
Output: ___________________
int main() {
int x = 10;
func(x);
cout << x << endl;
return 0;
}
```
Output: ___________________
```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: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
int c = a & b;
cout << c << endl;
return 0;
}
```
Output: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int arr[3] = {1, 2, 3};
cout << arr[3] << endl;
return 0;
}
```
Error: ___________________
```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: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 10;
swap(x, y);
cout << x << " " << y << endl;
return 0;
}
```
Output: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << (x > 2 ? x : 2) << endl;
return 0;
}
```
Output: ___________________
```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: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int x = 10;
cout << --x << endl;
return 0;
}
```
Output: ___________________
```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: ___________________
```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: ___________________
```cpp
#include <iostream>
using namespace std;
class Base {
public:
void display() {
cout << "Base class" << endl;
}
};
int main() {
Derived d;
d.display();
return 0;
}
```
Output: ___________________
```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: ___________________
```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: ___________________
```cpp
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Base class" << endl;
}
};
int main() {
Base* b = new Derived();
b->display();
return 0;
}
```
Output: ___________________
```cpp
#include <iostream>
using namespace std;
class Test {
public:
Test() {
cout << "Constructor" << endl;
}
~Test() {
cout << "Destructor" << endl;
}
};
int main() {
Test t;
return 0;
}
```
Output: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int x = 10;
int y = x++;
cout << y << endl;
return 0;
}
```
Output: ___________________
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: ___________________
```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: ___________________
```cpp
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base Constructor" << endl;
}
~Base() {
cout << "Base Destructor" << endl;
}
};
int main() {
Derived d;
return 0;
}
```
Output: ___________________
```cpp
#include <iostream>
using namespace std;
void func() {
static int x = 0;
x++;
cout << x << endl;
}
int main() {
func();
func();
return 0;
}
```
Output: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
cout << add(5, 10) << endl;
cout << add(2.5, 3.5) << endl;
return 0;
}
```
Output: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int& y = x;
y = 10;
cout << x << endl;
return 0;
}
```
Error: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << &x << endl;
return 0;
}
```
Output: ___________________
```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: ___________________
```cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 10;
swap(x, y);
cout << x << " " << y << endl;
return 0;
}
```
Output: ___________________
```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: ___________________
class Test {
public:
int x;
Test() : x(10) {}
};
int main() {
Test t;
cout << t.x << endl;
return 0;
}
```
Error: ___________________