What are inline functions?
1. Treated like macro definitions by C++ compiler.
2. Meant to be used if there’s a need to repetitively execute a small block if code which is smaller.
3. Always evaluates every argument once.
4. Defined in header file.
5. Avoids function call overload because calling a function is slower than evaluating the equivalent expression.
6. It’s a request to the compiler, the compiler can ignore the request.
Difference between C structure and C++ structure?
C++ places greater emphasis on type checking, compiler can diagnose every diff between C and C++1. structures are a way of storing many different values in variables of potentially diff types under under the same name
2. classes and structures make program modular, easier to modify make things compact
3. useful when a lot of data needs to be grouped together
4. struct Tag {…}struct example {Int x;}example ex; ex.x = 33; //accessing variable of structure
5. members of a struct in C are by default public, in C++ private
6. unions like structs except they share memory – allocates largest data type in memory - like a giant storage: store one small OR one large but never both at the same time
7. pointers can point to struct:
8. C++ can use class instead of struct (almost the same thing) - difference: C++ classes can include functions as members
9. members can be declared as: private: members of a class are accessible only from other members of their same class; protected: members are accessible from members of their same class and friend classes and also members of their derived classes; public: members are accessible from anywhere the class is visible
10. structs usually used for data only structures, classes for classes that have procedures and member functions
11. use private because in large projects important that values not be modified in an unexpected way from the POV of the object
12. advantage of class declare several diff objects from it, each object of Rect has its own variable x, y AND its own functions
13. concept of OO programming: data and functions are properties of the object instead of the usual view of objects as function parameters in structured programming
Difference between calloc and malloc?
malloc: allocate n bytes
calloc: allocate m times n bytes initialized to 0
calloc: allocate m times n bytes initialized to 0
Difference between printf and sprintf?
sprintf: a function that puts together a string, output goes to an array of char instead of stdoutprintf: prints to stdout
What is the difference between = and == in C?
“=” is an assignment operator while, “==” is comparative operatora=b; //b’s value is stored in a
if( a==b) // a’s value is compared with b
What is the difference between superclass and subclass?
A super class is a class that is inherited whereas sub class is a class that does the inheriting. What is difference between overloading and overriding?
a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.
d) Overloading must have different method signatures whereas overriding must have same signature.
How many ways can an argument be passed to a subroutine?
An argument can be passed in two ways. They are Pass by Value and Passing by Reference.Passing by value: This method copies the value of an argument into the formal parameter of the subroutine.
Passing by reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter.
What do you mean by multiple inheritance in C++ ?
Multiple inheritance is a feature in C++ by which one class can be of different types. Say class teachingAssistant is inherited from two classes say teacher and Student. What do you mean by virtual methods?
Virtual Methods are used to use the polymorphisms feature in C++. Say class A is inherited from class B. If we declare say function f() as virtual in class B and override the same function in class A then at runtime appropriate method of the class will be called depending upon the type of the object. How many ways are there to initialize an int with a constant?
Two.There are two formats for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.
int foo = 123;
int bar (123);
Explain the scope resolution operator.
It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope. In C, why is the void pointer useful? When would you use it?
The void pointer is useful becuase it is a generic pointer that any pointer can be cast into and back again without loss of information. In C, what is the difference between a static variable and global variable?
A static variable declared outside of any function is accessible only to all the functions defined in the same file (as the static variable). However, a global variable can be accessed by any function (including the ones from different files). What methods can be overridden in Java?
In C++ terminology, all public methods in Java are virtual. Therefore, all Java methods can be overwritten in subclasses except those that are declared final, static, and private. In C++, what is the difference between method overloading and method overriding?
Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
What is pure virtual function?
A class is made abstract by declaring one or more of its virtual functions to be pure. A pure virtual function is one with an initializer of = 0 in its declaration How can you tell what shell you are running on UNIX system?
You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID. How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle. What is polymorphism?
Polymorphism is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects. How do you write a function that can reverse a linked-list?
void reverselist(void){
if(head==0)
return;
if(head->next==0)
return;
if(head->next==tail)
{
head->next = 0;
tail->next = head;
}
else
{
node* pre = head;
node* cur = head->next;
node* curnext = cur->next;
head->next = 0;
cur->next = head;
for(; curnext!=0; )
{
cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
}
curnext->next = cur;
}
}
Write a fucntion that will reverse a string.
char *strrev(char *s){
int i = 0, len = strlen(s);
char *str;
if ((str = (char *)malloc(len+1)) == NULL) /*cannot allocate memory */
err_num = 2;
return (str);
}
while(len)
str[i++]=s[?len];
str[i] = NULL;
return (str);
}
What is the difference between Stack and Queue?
Stack is a Last In First Out (LIFO) data structure.
Queue is a First In First Out (FIFO) data structure
Queue is a First In First Out (FIFO) data structure
Suppose a 3-bit sequence number is used in the selective-reject ARQ, what is the maximum number of frames that could be transmitted at a time?
If a 3-bit sequence number is used, then it could distinguish 8 different frames. Since the number of frames that could be transmitted at a time is no greater half the numner of frames that could be distinguished by the sequence number, so at most 4 frames can be transmitted at a time. Describe Stacks and name a couple of places where stacks are useful.
A Stack is a linear structure in which insertions and deletions are always made at one end, called the top. This updating policy is called last in, first out (LIFO). It is useful when we need to check some syntex errors, such as missing parentheses. Describe one simple rehashing policy.
The simplest rehashing policy is linear probing. Suppose a key K hashes to location i. Suppose other key occupies H[i]. The following function is used to generate alternative locations:rehash(j) = (j + 1) mod h
where j is the location most recently probed. Initially j = i, the hash code for K. Notice that this version of rehash does not depend on K.
Write the psuedo code for the Depth first Search.
dfs(G, v) //OUTLINEMark v as “discovered”
For each vertex w such that edge vw is in G:
If w is undiscovered:
dfs(G, w); that is, explore vw, visit w, explore from there
as much as possible, and backtrack from w to v.
Otherwise:
“Check” vw without visiting w.
Mark v as “finished”.
No comments:
Post a Comment