Hot Topics
Seimens Solution
Technical Round
- Question 1
Copy constructor.
- Answer
A copy constructor is a special constructor in a class that creates a new object as a copy of an existing object. In many programming languages, including C++ and Java, the copy constructor is used to initialize an object from another object of the same class.
Here is an example of a copy constructor in C++:
#include <iostream>
class Point {
public:
int x, y;
// default constructor
Point() : x(0), y(0) {}
// copy constructor
Point(const Point &p) : x(p.x), y(p.y) {}
};
int main() {
Point p1;
p1.x = 1;
p1.y = 2;
// create a new object as a copy of p1
Point p2 = p1;
std::cout << "p1: " << p1.x << ", " << p1.y << std::endl;
std::cout << "p2: " << p2.x << ", " << p2.y << std::endl;
return 0;
}
/*
OUTPUT - p1: 1, 2
p2: 1, 2
*/
In this example, the Point
class has two integer attributes x
and y
to represent a point in 2D space. The class has two constructors: a default constructor that initializes the point to (0, 0), and a copy constructor that takes a const
reference to another Point
object and initializes the new object as a copy of the original object.
When the line Point p2 = p1;
is executed, the copy constructor is called to create a new Point
object p2
as a copy of the existing Point
object p1
.
- Question 2
Operator overloading.
- Answer
Operator overloading is a feature in some programming languages that allows operators (such as +
, -
, *
, /
, etc.) to have different meanings depending on the context in which they are used. In other words, the same operator can perform different actions depending on the data types of the operands.
For example, in many programming languages, the +
operator is used to add numbers, but it can also be used to concatenate strings. In C++, you can define the behavior of the +
operator for a custom data type by overloading the operator:
#include <iostream>
class Complex {
public:
int real, imag;
Complex operator+ (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
};
int main() {
Complex c1, c2, c3;
c1.real = 1;
c1.imag = 2;
c2.real = 3;
c2.imag = 4;
c3 = c1 + c2;
std::cout << c3.real << " + " << c3.imag << "i" << std::endl;
return 0;
}
/*
OUTPUT - 4 + 6i
*/
In this example, the Complex
class has two integer attributes real
and imag
to represent a complex number. The operator+
function is a member function of the Complex
class that overloads the +
operator to perform addition of two complex numbers. The function takes one argument, a Complex
object, and returns a new Complex
object representing the sum of the two complex numbers.
Note that operator overloading must be used carefully to avoid unexpected behavior and ensure the intended meaning of the overloaded operator is clear to the reader of the code.
- Question 3
There is cube of size 3x3x3 unit. All of its 6 faces are painted. You cut that cube into 27 cubes of 1x1x1 size of cubes. How many cubes will have no face painted.
- Answer
A cube of size 3x3x3 has 27 smaller cubes of size 1x1x1. Of these 27 cubes, 8 cubes are located at the corners of the larger cube and have 3 faces painted. 12 cubes are located along the edges of the larger cube and have 2 faces painted. The remaining 6 cubes are located at the center of each face of the larger cube and have 1 face painted.
Therefore, there will be 1 cube that has no face painted.
- Question 4
Difference between heap & stack.
- Answer
Heap | Stack |
1. Heap memory is used by all the parts of the application. | Stack memory is used only by one thread of execution. |
2. Objects stored in heap are globally accessible. | Stack memory cannot be accessed by other thread. |
3. Memory management is based on the generation associated with each other. | Follows LIFO manner to free memory. |
4. Heap memory lives from the start till the end of application execution. | Stack memory exists until the end of execution of the thread. |
5. Whenever an object is created, it’s always stored in the Heap space. | Stack memory only contains local primitive and reference variables to objects in heap space. |
- Question 5
Tell me about your projects.
- Answer
Tell about your project
- Question 6
Difference between malloc and calloc.
- Answer
Calloc() | Malloc() |
1. Indicates contiguous memory allocation. | Indicates memory allocation. |
2. It creates more than one block of memory to a single variable. | It creates one block of memory of a fixed size. |
3. It takes two arguments. | It takes one argument. |
4. It is slower than malloc(). | It is faster than calloc(). |
- Question 7
OOPs concept.
- Answer
OOP stands for Object-Oriented Programming, a programming paradigm that focuses on objects as the main building block for creating programs. It is based on the concepts of encapsulation, inheritance, polymorphism, and abstraction. Encapsulation refers to the bundling of data and methods that operate on that data within a single unit, or object. Inheritance refers to the ability to create a new class based on an existing class, inheriting its attributes and methods. Polymorphism refers to the ability of an object to take on multiple forms. Abstraction refers to the idea of using objects to represent real-world concepts, hiding their internal complexity.
- Question 8
Assume you have 5 boxes, each box have some different number of beans in it each bean weighs 10 grams except the beans in one jar ,in that jar each bean has a weight of 9 grams (let’s say some defected Seeds) now you can weigh only once and find out the jar with defected beans (you can weigh as you wish but only once)
- Answer
One possible solution would be to weigh three beans from each of the five boxes, and then weigh all 15 beans together. If all the beans are of weight 10 grams, then the combined weight of 15 beans would be 150 grams. If one of the boxes contains 9-gram beans, the combined weight of 15 beans would be less than 150 grams. By finding the difference between the expected weight and the actual weight, you can determine which box contains the defected beans.
- Question 9
Write a function that reveres a string, using a double pointer.
- Answer
Here’s an implementation of a function that reverses a string using a double pointer in C:
#include <stdio.h>
#include <string.h>
void reverse_string(char *str) {
char *end = str + strlen(str) - 1;
while (str < end) {
char temp = *str;
*str++ = *end;
*end-- = temp;
}
}
int main() {
char str[] = "Hello, World!";
reverse_string(str);
printf("Reversed string: %s\n", str);
return 0;
}
/*
OUTPUT - Reversed string: !dlroW ,olleH
*/
In this implementation, str
is a pointer to the first character of the string, and end
is a pointer to the last character of the string. The while
loop iterates until str
pointer is less than end
pointer. In each iteration, the temp
variable is used to store the value at the str
pointer, then the value at str
is replaced with the value at end
, and finally, the value at end
is replaced with the value stored in temp
. This effectively swaps the values at str
and end
and the pointers are incremented and decremented respectively, until the str
and end
pointers cross each other in the middle of the string.
- Question 10
Describe classes and objects by writing a code?
- Answer
Here’s an example in Python that demonstrates the concepts of classes and objects:
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_description(self):
return f"{self.year} {self.make} {self.model}"
# Creating objects of the Car class
my_car = Car("Toyota", "Camry", 2020)
your_car = Car("Honda", "Civic", 2022)
# Accessing the object attributes
print(my_car.make) # Output: Toyota
print(your_car.model) # Output: Civic
# Calling object methods
print(my_car.get_description()) # Output: 2020 Toyota Camry
print(your_car.get_description()) # Output: 2022 Honda Civic
#OUTPUT - Toyota
# Civic
# 2020 Toyota Camry
# 2022 Honda Civic
In this example, the Car
class is defined with three attributes make
, model
, and year
, and a method get_description()
. The __init__
method is a special method in Python classes that is automatically called when an object of the class is created. It allows you to initialize the attributes of an object when it is created.
Two objects of the Car
class are created: my_car
and your_car
. Each object has its own set of attributes that can be accessed using dot notation (e.g. my_car.make
). The methods of the class can be called on objects of the class in the same way (e.g. my_car.get_description()
).
- Question 11
Can you fulfil necessary functions using these specialized software programs?
- Answer
Yes, specialized software programs can be used to fulfill various functions, depending on their purpose and design. Some examples of specialized software programs include:
Image editing software, such as Adobe Photoshop, for editing and manipulating digital images
Video editing software, such as Adobe Premiere Pro, for editing and producing digital videos
Audio editing software, such as Audacity, for editing and processing audio files
CAD software, such as AutoCAD, for creating and editing computer-aided designs
Accounting software, such as QuickBooks, for managing financial records and generating financial reports
Project management software, such as Asana, for organizing and tracking project tasks and timelines
Anti-virus software, such as Kaspersky, for protecting computers from malicious software and cyber threats
These are just a few examples, but there are many other specialized software programs available for various functions.