Programming Basic Interview Questions

1. What is a regular expression?
A regular expression is a special sequence of characters that help a user match or find other strings (or sets of strings), using a special syntax. It is one of the most powerful concepts used for pattern matching within strings. Regular expressions are widely used in languages like Perl, Python, Tcl etc.
2. What is the difference between a heap and a stack?
Stack is a special region of memory that stores temporary variables created by a function. Every time a function declares a new automatic variable, it is pushed to the stack and every time the function exits, all of the variables pushed on the stack are deleted. All local variables uses stack memory. Stack memory is managed automatically and also has a size limitation. If stack runs out of memory, we get stack overflow error.
Heap on the other hand is a region of memory that needs to be managed. The programmer has to allocate and free memory. These are typically used to store static variables as well as for objects. It is slightly slow compared to stack and is referenced through pointers. Also unlike stack, variables stored on heap can be referenced from anywhere in the program. Size of heap can be changed. Heap can have fragmentation problem when available memory is stored as disconnected blocks.
3. What is the difference between ++a and a++ in any programming language?
++a first increment the value of “a” and then returns a value referring to “a”. Hence, if “++a” is assigned to a variable, then incremented value of “a” will be used.
a++ first returns value of “a” (which is current value of “a”) and then increments “a”. Hence, if “a++” is assigned to a variable, then old value of “a” will be used in the assignment.
4. What is a pointer? Explain the concept of pointer.
Pointer is a variable whose value is an address of another variable. A pointer contains direct address of a memory location. The asterisk sign * is used to denote a pointer.
int *p; tells the compiler that variable “p” is a pointer, whose value is an address of a memory location where an integer variable is stored.
Similarly, float *f; tells the compile that variable “f” is a pointer, whose value is an address of a memory location where a float variable is stored.
Example:
int a = 20;
int *b;
int c;
b = &a;
c = *b;
printf(“b=%d and c=%d\n”,b,c);
Here, value of variable “a” is 20. “&a” denotes memory address where value of the variable ”a” is stored. Let us assume that variable “a” is stored at a memory location “0x1000” (4096 in decimal). Since pointer “b” is assigned address of “a”, value of variable “b” would be “4096”. Further, variable “c” would be equal to value of variable “a”. Therefore, output of above printf would be: b=4096 and c=20 .
5. Explain the concept of Pass by Value and Pass by Reference in C?
Pass by Value: In this case, a copy of the “Value” of actual parameter is made in the memory, and is assigned to a new variable. This means that any changes made to the value of the new variable inside the function are local to that function and doesn’t impact
original value of the actual parameter.
Pass by Reference: In this case, a copy of the “Address” of actual parameter is made in the memory and is assigned to a new variable. This is used when the function makes changes to the variable which will then also reflect in the original parameter value. Parameters that normally consume more memory are normally passed by reference to avoid creating copies local to function. Example: arrays are normally passed by reference.
6. Explain the difference between & and && operators in C/C++?
& is a bitwise AND operator while && is a logical AND operator.
Logical operators work with boolean values - true (1) and false (0), and return a boolean value.
Bitwise operators perform bit operation on each of the bit and return a bit value.
Bitwise operator usage: if a =10 and b = 6, a & b will return 2 (4’b1010 & 4’b0110 = 4’b0010)
Logical Operator: If a=10 and b=6, then the following expression will return true as it operates on two boolean values which are true in this example. 
c = (a==10) && (b==6);
7. What is a kernel?
A Kernel is a computer program that manages Input/output requests from software and translates these requests into CPU instructions or other electronic instructions for the computer.
8. What is the difference between a class and object?
A class is a set of attributes and associated behaviour that can be grouped together. An object is an instance of the class which then represents a real entity with attributes and behaviour. The attributes can be represented using class data members while the behavior can be represented using methods.
9. What is Polymorphism?
Polymorphism means the ability to assume several forms. In OOP context, this refers to the ability of an entity to refer to objects of various classes at run time. This is possible with the concept of inheritance and virtual functions in System Verilog (and with the concept of function and operator overloading, which are present in C++). Depending on the type of object, appropriate method will be called from the corresponding class.
10. What is ‘this’ pointer with reference to class?
this pointer is a special pointer that can be used to reference the current object of a class
inside the class scope.
11. What is the Value and Size of a NULL pointer?
NULL pointer can be defined as: int *a = NULL;
Value of a NULL pointer is 0.
12. What is the difference between a Class and a Struct in System Verilog?
In System Verilog, both class and struct are used to define a bundle of data types based on some functionality to be performed. However, a struct is an integral type and when it is declared, the necessary memory is allocated. On the other hand, a class is a dynamic type and once you declare a class, you only have a class handle referenced to null. Memory allocation happens only when an actual object of the class is created.
13. What are public, private and protected members?
1. Private data members of a class can only be accessed from within the class. These data members will not be visible in derived classes
2. Public members can be accessed from within the class as well as outside the class also. These are also visible in derived classes
3. Protected data members are similar to private members in the sense that they are only accessible within the class. However unlike private members, these are also visible in derived class.
14. How does a Structure and a Union differ in terms of memory allocation in C/C++?
Structs allocate enough space to store all of the fields/members in the struct. The first one is stored at the beginning of the struct, the second is stored after that, and so on. Unions only allocate enough space to store the largest field listed, and all fields are stored at the same space. This is because in a union, at a time only one type of enclosed variable can be used unlike struct where all the enclosed variables can be referenced.
15. What is a Stack and how does a Stack differ from FIFO?
Stack is a data structure. Stack is “Last In First Out” whereas FIFO is “First In First Out”.
SHARE

vlsi4freshers

Hi I’m Designer of this blog.

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment