0% found this document useful (0 votes)
5 views3 pages

Java & C++ Internship Test MCQs

The document outlines an internship interview test focused on Java and C++, consisting of multiple-choice questions (MCQs) and coding tasks. It includes questions on Java features such as runtime polymorphism and class inheritance, as well as C++ topics like function overloading and memory allocation. Coding tasks require the creation of classes in both languages to demonstrate understanding of object-oriented programming principles.

Uploaded by

vijaydisale010
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)
5 views3 pages

Java & C++ Internship Test MCQs

The document outlines an internship interview test focused on Java and C++, consisting of multiple-choice questions (MCQs) and coding tasks. It includes questions on Java features such as runtime polymorphism and class inheritance, as well as C++ topics like function overloading and memory allocation. Coding tasks require the creation of classes in both languages to demonstrate understanding of object-oriented programming principles.

Uploaded by

vijaydisale010
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

Internship Interview Test: Java & C++

Part 1: MCQ Quiz (Java + C++)

Java MCQs
1. 1. What is the output of the following code?
```java
String s1 = "hello";
String s2 = new String("hello");
[Link](s1 == s2);
```

 a) true
 b) false
 c) Compilation Error
 d) Runtime Error

2. 2. What is the size of an `int` in Java?

 a) 2 bytes
 b) 4 bytes
 c) 8 bytes
 d) Depends on OS

3. 3. Which of the following is used for runtime polymorphism?

 a) Method Overloading
 b) Constructor Overloading
 c) Method Overriding
 d) None of the above

4. 4. Which keyword is used to inherit a class in Java?

 a) implements
 b) inherits
 c) extends
 d) super

C++ MCQs
5. 5. What is the output of the following code?
```cpp
int a = 10;
int &b = a;
b = 20;
cout << a;
```

 a) 10
 b) 20
 c) Compilation Error
 d) Undefined

6. 6. Which of the following supports function overloading?

 a) Java only
 b) C++ only
 c) Both
 d) None

7. 7. What is the use of a virtual function in C++?

 a) Memory optimization
 b) Achieving runtime polymorphism
 c) Faster execution
 d) None of the above

8. 8. Which of these is a correct way to allocate memory dynamically in C++?

 a) int *p = malloc(sizeof(int));
 b) int *p = new int;
 c) int p = new int();
 d) int p = malloc(int);

Part 2: Coding Test

Java Coding Questions


9. 1. Class and Methods

Create a class `Student` with attributes: name, roll number, and marks. Write methods to:
- Accept student details
- Display student details

10. 2. Inheritance Example

Write a Java program with a base class `Vehicle` and a derived class `Car`. Add methods like
`startEngine()` and `displayDetails()`.

C++ Coding Questions


11. 3. Basic Class Example
Write a C++ program to define a class `Rectangle` with private members `length` and
`width`. Include methods to:
- Accept input
- Calculate and display area

12. 4. Constructor Overloading

Write a C++ program to demonstrate constructor overloading with a class `Box` that can be
initialized:
- Without any parameters
- With one parameter
- With two parameters

Common questions

Powered by AI

Constructor overloading allows for flexible initialization of objects in C++. For a class like `Box`, it provides the ability to create objects with no initial size, a default size, or with specific dimensions. This level of flexibility is crucial in real-world applications where different initialization parameters might be needed depending on the context or data availability .

Virtual functions in C++ enable runtime polymorphism by allowing derived classes to override methods in base classes. When a function is declared virtual in a base class, C++ uses a vtable (virtual table) to resolve method calls at runtime rather than at compile time. This means the appropriate method is called based on the object's actual type during execution, allowing for more dynamic and flexible code .

In Java, the `==` operator compares the reference memory addresses of objects, not their values. Thus, when two `String` objects are compared using `==`, it checks whether they refer to the exact same object instance. In the given example, `s1` is a literal in the string pool, while `s2` is a new `String` object created on the heap with `new`. Hence, `s1` and `s2` do not refer to the same memory location, leading to `s1 == s2` evaluating to `false` .

Method overriding in Java is the mechanism that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is a key aspect of runtime polymorphism, where the method that gets invoked is determined at runtime, allowing for dynamic method dispatch .

`String.equals()` in Java checks for value equality, meaning it compares the actual contents of the `String` objects. Conversely, `==` checks if the references point to the same memory location (reference equality). Thus, `String.equals()` should be used when we need to know if two strings have the same characters in the same order, whereas `==` is more about checking if two references are exactly the same object .

Class inheritance in Java allows for code reuse and the creation of a more organized and maintainable codebase. In a structure with a `Vehicle` base class and a `Car` derived class, common properties and methods related to vehicles can be defined in the `Vehicle` class, ensuring code reuse. The `Car` class can inherit these properties and methods, and also add specific functionalities related to cars, achieving an efficient and DRY (Don't Repeat Yourself) design pattern .

In Java, `extends` is used to inherit a class, meaning the subclass gains access to the public and protected fields and methods of the superclass. This is a direct class inheritance. On the other hand, `implements` is used with interfaces, allowing a class to implement multiple interfaces, thus committing to provide concrete implementations for the abstract methods defined in those interfaces .

In C++, `new` is the correct way to allocate memory dynamically because it not only allocates memory but also calls the constructor if it's for a class object, initializing the memory. In contrast, `malloc` simply allocates a block of memory and returns a void pointer, without any initialization. This distinction is crucial for object-oriented programming in C++ where constructor behavior is needed .

The output of the code will be `20`. In C++, when a reference is declared, like `int &b = a;`, `b` becomes an alias for `a`. This means any changes made to `b` are reflected directly on `a`. Therefore, updating `b` to `20` also updates `a`, so `cout << a;` will output `20` .

Function overloading is supported in both C++ and Java, allowing methods with the same name to coexist as long as their parameter lists differ. However, in C++, the return type alone cannot distinguish overloaded functions. Java enforces the same rule but also has stricter type casting rules which impact method resolution. C++ provides more flexibility with default arguments, and its support for templates enhances overloading capabilities in ways Java does not directly support .

You might also like