Java & C++ Internship Test MCQs
Java & C++ Internship Test MCQs
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 .