C and C++
C and C++
#include <stdio.h>
int main()
{
int i = 3;
int l = i / -2;
int k = i % -2;
printf("%d %d\n", l, k);
return 0;
}
a) Compile time error
b) -1 1
c) 1 -1
d) Implementation defined
Answer b
Q2. What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 1, y = 0, z = 5;
int a = x && y || z++;
printf("%d", z);
}
a) 6
b) 5
c) 0
d) Varies
Answer a
Q3.What is the output of this C code?
#include <stdio.h>
int main()
{
int x = 1, y = 0, z = 3;
x > y ? printf("%d", z) : return z;
}
a) 3
b) 1
c) Compile time error
d) Run time error
answer c
Q4. What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 1, z = 3;
int y = x << 3;
printf(" %d\n", y);
}
a) -2147483648
b) -1
printf("hi");
else
printf("no");
}
a) hi
b) hello
c) no
d) None of the mentioned
answer a
Q7. Analyze the given code and state the output
#include <stdio.h>
int num = 10;
main()
{
int num = 20;
{
printf("\n %d", num);
}
printf(" %d", num);
}
a.10 20
b. 30 30
c. 20 10
d. None of above
Answer c
10 ABC
20 ABC
Abc 10
error
Answer-d
Structure must be displayed before the function
{
int arr[ ] = {1,2,3,4,5};
int i,*parr = arr;
for(i = 0;i < 5;i++) {
printf(" %d ",*arr);
++parr; }
}
a)
b)
c)
d)
00000
010101
11111
none
Answer: c
because parr is being incremented and we are printing the value of arr.
Q11. What is the output of given code
main()
{
int arr[ ] = {1,2,3,4,5};
int i,*parr = arr;
for(i = 0;i < 5;i++) {
printf(" %d ",*parr);
++parr; }
}
a)
b)
c)
d)
Ans: c
0111000
010101
12345
None of above
0111000
00131
12345
None of above
Ans: b
Q13. What is the output of given code
main()
{
char *str;
printf("%d %d ",sizeof(*str), sizeof(str));
}
a)
b)
c)
d)
12
21
34
221
Answer: a
Since str is a pointer to a character type, its value needs 1 byte in memory and the address of this value will be
of 2 bytes.
Q14. What is the output of given code
main()
{
char str[]="Hello World";
show(str);
}
void show(char *str)
{
printf("%s",str);
}
a)
b)
c)
d)
Hello world
Hello World
HELLO WORLD
error
Ans:d ERROR. The default argument type and return type of a function is int. So a function that has any
other type of arguments and/or return type must be declared before it is called. In this case, the return type is
void and argument type is char and there is no declaration statement which gives the details to the compiler
in advance. Hence it is an error because the compiler assumes that the function will accept and return an
integer value.
--1
--2
%d%d
error
Ans: d ERROR. Unary operator can be applied to variables and not to constants. Since 2 is a
constant, therefore the program generates error.
Q16. What is the output of given code
#define int char
main()
{
int num = 65;
printf("%d",sizeof(num));
}
a)
b)
c)
d)
1
2
6
0
Ans: a
Because num is actually a char, not an int. See the #define statement
Res=0
Res=1
Res=-1
None of above
Ans: a
Because res = 5 in line 1. In line 2, when we write !res, here ! is the negation operator which converts the value
of res to Zero. Now, 0 is not > Than 5.Therefore the value of res = 0.
1089
1098
1000
1111
Ans: b
Because p is pointing to the third character in str which is \n. The ascii value of new line character is 10. pstr
stores the address of the first character in the str which is a. The value of *pstr is being incremented,
therefore it is 98. The ascii value of a + 1 = 97 +1 = 98
Q19. The first and second arguments of fopen are
a) A character string containing the name of the file & the second argument is the mode.
b) A character string containing the name of the user & the second argument is the mode.
c) A character string containing file poniter & the second argument is the mode.
d) None of the mentioned of the mentioned
Answer:a
Q20.For binary files, a ___ must be appended to the mode string.
a) Nothing
b) b
c) binary
d) 01
Answer B
Q21If there is any error while opening a file, fopen will return
a) Nothing
b) EOF
c) NULL
d) Depends on compiler
Answer C
d) None
Answer a
Although arr is declared as a 3-D array, the third dimension is not initialized. Therefore, *p
will contain garbage value as no value exists for arr[2][2][2]. *q contains the address of the fi rst
element in the array, that is 1 in this case.
Q29. Why should one use standard library functions instead of writing new one?
The standard library functions have three advantages: they work, they're efficient, and they're portable.
They work: Your compiler vendor probably got them right. More important, the vendor is likely to have done
a thorough test to prove they're right, more thorough than you probably have time for. (There are expensive
test suites to make that job easier.)
They're efficient: Good C programmers use the standard library functions a lot, and good compiler vendors
know that. There's a competitive advantage for the vendor to provide a good implementation. When
competing compilers are compared for efficiency, a good compiler implementation can make all the
difference. The vendor has more motivation than you do, and probably more time, to produce a fast
implementation.
They're portable: In a world where software requirements change hourly, the standard library functions do
the same thing, and mean the same thing, for every compiler, on every computer. They're one of the few
things you, as a C programmer, can count on.
The funny thing is, one of the most standard pieces of information about the standard library is hard to find.
For every function, there's one header file (or, rarely, two) that guarantees to give you that function's
prototype. (You should always include the prototype for every function you call;) What's funny? That header
file might not be the file that actually contains the prototype. In some (sad!) cases, it's not even the header file
recommended by the compiler manual. The same is true for macros, typedefs, and global variables.
Q30. What is the difference between realloc() and free()?
The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results
occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will
occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to
the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer
specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and
not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is
not a valid pointer.
Q31. What is function overloading and operator overloading?
Function overloading: C++ enables several functions of the same name to be defined, as long as these
functions have different sets of parameters (at least as far as their types are concerned). This capability is
called function overloading. When an overloaded function is called, the C++ compiler selects the proper
function by examining the number, types and order of the arguments in the call. Function overloading is
commonly used to create several functions of the same name that perform similar tasks but on different data
types.
Operator overloading allows existing C++ operators to be redefined so that they work on objects of userdefined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant
facade that doesn't add anything fundamental to the language (but they can improve understandability and
reduce maintenance costs).
Q32. What is the difference between declaration and definition?
The declaration tells the compiler that at some later point we plan to present the definition of this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j > =0; j--) //function body
cout << *;
cout << endl; }
Q33. What are the advantages of inheritance?
It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven
and debugged high-quality software, thus reducing problem after a system becomes functional.
Q34. What do you mean by inline function?
The idea behind inline functions is to insert the code of a called function at the point where the function is
called. If done carefully, this can improve the application's performance in exchange for increased compile
time and possibly (but not always) an increase in the size of the generated binary executables.
Q35. Write a short code using C++ to print out all odd number from 1 to 100 using a for loop
for( unsigned int i = 1; i < = 100; i++ )
if( i & 0x00000001 )
cout << i << \",\";
Q36. What is public, protected, private?
Public, protected and private are three access specifier in C++.
Public data members and member functions are accessible outside the class.
Protected data members and member functions are only available to derived classes.
Private data members and member functions cant be accessed outside the class. However there is an
exception can be using friend classes.
Write a function that swaps the values of two integers, using int* as the argument type.
void swap(int* a, int*b) {
int t;
t = *a;
*a = *b;
*b = t;
}
Q37. What is virtual constructors/destructors?
Answer Virtual destructors:
If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a baseclass pointer to the object, the base-class destructor function (matching the pointer type) is called on the
object.
There is a simple solution to this problem declare a virtual base-class destructor.
This makes all derived-class destructors virtual even though they dont have the same name as the base-class
destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a baseclass pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor:
Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.
Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete
operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is
class CIRCLE{
public int r;
public virtual Draw() { this->drawCircle(0,0,r); }
};
class SQURE
public int a;
public virtual Draw() { this->drawRectangular(0,0,a,a); }
};
Each object is driven down from SHAPE implementing Draw() function in its own way.
Q47. What is an object?
Object is a software bundle of variables and related methods. Objects have state and behavior.
Q48. 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.
Q49. What do you mean by inheritance?
Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes.
The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of
its own.
Q50. Describe PRIVATE, PROTECTED and PUBLIC the differences and give examples.
class Point2D{
int x; int y;
public int color;
protected bool pinned;
public Point2D() : x(0) , y(0) {} //default (no argument) constructor
};
Point2D MyPoint;
You cannot directly access private data members when they are declared (implicitly) private:
d.All
ANS: b
Q56. Why do we use typedef?
a. To declare new name for existing data type
b.To create new integers
c. To create new characters
d. To create new array
ANS : a
Q57.The method of accessing the members of union and structure is
a)same
b)different
c.)wecant access the members of union
d)We cant access the members of structure
ANS : a
Q58. Bit field is
(a) A way to specify number of bits in structure/union
(b) A way to name new data types
(c)A way to create name of existing data types
(d)All of these
Ans : a
Q59. Bit field is used in
(a) Structure
(b) pointer
(C)Function
(d)None
Ans :a
Q60. Can typedef be used with structure?
a)Yes
b) No
(c ) No because it will be a run time error
(d ) No because it will be a compile time error
ANS : A
ii . 3, garbage,garbage
iii. Garbage value three times
iv. 3,2,1
ANS : i
Q65. Which operator is used to access the elements of union using union variable
(i) &
(ii)dot
(iii) *
(iv)None
ans ii
Q66. Union is similar to structure according to which statement
(i) both use the keyword struct
(ii)both use the keyword union
(iii)both can contain different type of data
(iv)bothcant have pointers
ans : iii
Q67. A union is
(i) derived data type
(ii)fundamental data type
(iii) user defined data type
(iv) none
ans: iii
Q68. Which one of the following is not a user defined data type
(i) structure
(ii)union
(iii)array
(iv) All are user defined
ans : iii
Q69. Find the output of given code?
#include<stdio.h>
int main()
{ union A
{ int x;
float y; };
union A u;
u.x=300;
u.y=22.22;
printf("%f",u.y);
return 0;
}
(a) 300
(b) garbage value
(c)22.22
d. None
ans : C
Q70.What is the output of given code
#include<stdio.h>
int main()
{
union A
{ int x;
float y;
};
union A u;
u.x=300;
u.y=22.22;
printf("%f",u.x);
return 0;
}
(a) 300
(b) 0 or garbage value
(c)22.22
(d)None
ans: b
Q71.choose the incorrect statement
(i)pointer cannot be created for union
(ii)structure can be used with union
ANS: iv
Q77.Suppose we have two structures struct a and struct b . Read the code and
choose the correct option
struct A { int a;};
struct B(struct A ob1;
int b;};
struct B ob2;
(i)to access a we will have to write ob2.ob1.a;
(ii)to access a we will have to write ob1.a;
(iii)to access a we will have to write ob1.ob2.a;
(iv)None
ANS: i
Q78.Suppose we have two structures struct a and struct b . Read the code and
choose the correct option
struct A { int a;};
struct B(struct A ob1;
int b;};
struct B ob2;
(i)to access int b we will have to write ob2.ob1.b;
(ii)to access int b we will have to write ob2.b;
(iii)(iii)to access int b we will have to write ob1.b;
(iv)None
ANS: ii
Q79. The elements of a union by default belongs to
(i)auto storage class
(ii)register storage class
(iii)static storage class
(iv) none
ans (i)
Q80.The difference between typedef and #define is
(i) The typedefis limited to giving symbolic names to types only where as#define
can be used to define alias for values as well
};
the maximum value that can be correctly stored in a is
(i)2 raised to the power 16
(ii) 2 raised to the power 15
(iii) 8
(iv) 7
ANS: iv
Q84. Consider the following code
struct {
int mynegativenumber:3;
};
the negative values which can be correctly stored in mynegativenumber are
(i)-4,-3,-2,-1
(ii) -3,-2,-1
(iii) negative infinity to -1
(iv) - 2 raised to the power 15 to -1
ANS: i
Q85. Read the following code (in turbo c)
struct student{
int a:3;
int b:2;
};
struct student s;
then the result of sizeof(s) will be
(i) 4
(ii)2
(iii)8
(iv)16
ans: ii
Q86.Find the output of given code
#include<stdio.h>
int main()
{t
ypedefintmyint;
myint a=5;
int b=4;
printf("%d",a==b);
return 0;
}
(i) 1
(ii)4
(iii)5
(iv)0
ans: IV
Q87. In turbo C (size of int =2 bytes) what will be the output of the following
#include<stdio.h>
int main()
{typedefintmyint;
myint a=5;
int b=4;
printf("%d",sizeof(a));
return 0;
}
(i)4
(ii)5
(iii)garbage value
(iv) 2
ANS: iv
Q88. What will be the output of the following
#include<stdio.h>
int main()
{t
ypedefintmyint;
typedef char mychar;
myint a=5;
mychar a='f';
printf("%d",sizeof(a));
return 0;
}
struct a{
int a;
};
struct a ob;
ob.a=5;
printf("%d",ob.a);
return 0;
}
(i) 5
(ii) 0
(iii) Error: structure name and variable name are same
(iv) garbage value
ANS: i
Q91. #include<stdio.h>
int main(){int a=6;
struct a{
int a;
};
struct a ob;
ob.a=5;
printf("%d",a);
return 0;
}
(i) 5
(ii) 6
(iii)garbage value
(iv) ERROR: because multiple declarations of a are conflicting
ans :ii
Q92. What will be the output of the following
#include<stdio.h>
int main( ){int a=6;
struct a{
int a;
};
union a{ int b;};
struct a ob;
union a ub;
ub.b=4;
ob.a=5;
printf("%d",a);
return 0;
}
(i) 4
(ii)5
(iii)6
(iv)Error : because of multiple declarations
Ans :iv