Google

Java Interview QuestionsLatest Placement papers

What is the protocol used by server and client ?

Can I modify an object in CORBA ?

What is the functionality stubs and skeletons ?

What is the mapping mechanism used by Java to identify IDL language ?

Diff between Application and Applet ?

What is serializable Interface ?

What is the difference between CGI and Servlet ?

What is the use of Interface ?

Why Java is not fully objective oriented ?

Why does not support multiple Inheritance ?

What it the root class for all Java classes ?

What is polymorphism ?

Suppose If we have variable ' I ' in run method, If I can create one or more thread each thread will occupy a separate copy or same variable will be shared ?

In servlets, we are having a web page that is invoking servlets username and password ? which is checked in the database ? Suppose the second page also If we want to verify the same information whether it will connect to the database or it will be used previous information?

What are virtual functions ?

Write down how will you create a binary Tree ?

What are the traverses in Binary Tree ?

Write a program for recursive Traverse ?

What are session variable in Servlets ?

What is client server computing ?

What is Constructor and Virtual function? Can we call Virtual function in a constructor ?

Why we use OOPS concepts? What is its advantage ?

What is the middleware ? What is the functionality of Webserver ?

Why Java is not 100 % pure OOPS ? ( EcomServer )

When we will use an Interface and Abstract class ?

What is an RMI?

How will you pass parameters in RMI ? Why u serialize?

What is the exact difference in between Unicast and Multicast object ? Where we will use ?

What is the main functionality of the Remote Reference Layer ?

How do you download stubs from a Remote place ?

What is the difference in between C++ and Java ? can u explain in detail ?

I want to store more than 10 objects in a remote server ? Which methodology will follow ?

What is the main functionality of the Prepared Statement ?

What is meant by static query and dynamic query ?

What are the Normalization Rules ? Define the Normalization ?

What is meant by Servlet? What are the parameters of the service method ?

What is meant by Session ? Tell me something about HTTPSession Class ?

How do you invoke a Servlet? What is the difference in between doPost and doGet methods ?

What is the difference in between the HTTPServlet and Generic Servlet ? Explain their methods ? Tell me their parameter names also ?

Have you used threads in Servlet ?

Write a program on RMI and JDBC using StoredProcedure ?

How do you sing an Applet ?

In a Container there are 5 components. I want to display the all the components names, how will you do that one ?

Why there are some null interface in java ? What does it mean ? Give me some null interfaces in JAVA ?

Tell me the latest versions in JAVA related areas ?

What is meant by class loader ? How many types are there? When will we use them ?

How do you load an Image in a Servlet ?

What is meant by flickering ?

What is meant by distributed Application ? Why we are using that in our applications ?

What is the functionality of the stub ?

Have you used any version control ?

What is the latest version of JDBC ? What are the new features are added in that ?

Explain 2 tier and 3 -tier Architecture ?

What is the role of the webserver ?

How have you done validation of the fields in your project ?

What is the main difficulties that you are faced in your project ?

What is meant by cookies ? Explain ?

Java database Placement Papers

  • What is the difference between an Abstract class and Interface ?
  • What is user defined exception ?
    • What do you know about the garbage collector ?
  • What is the difference between C++ & Java ?
  • Explain RMI Architecture?
  • How do you communicate in between Applets & Servlets ?
  • What is the use of Servlets ?
  • What is JDBC? How do you connect to the Database ?
  • In an HTML form I have a Button which makes us to open another page in 15 seconds. How will do you that ?
  • What is the difference between Process and Threads ?
  • What is the difference between RMI & Corba ?
  • What are the services in RMI ?
  • How will you initialize an Applet ?
  • What is the order of method invocation in an Applet ?
  • When is update method called ?
  • How will you pass values from HTML page to the Servlet ?
  • Have you ever used HashTable and Dictionary ?
  • How will you communicate between two Applets ?
  • What are statements in JAVA ?
  • What is JAR file ?
  • What is JNI ?
  • What is the base class for all swing components ?
  • What is JFC ?
  • What is Difference between AWT and Swing ?
  • Considering notepad/IE or any other thing as process, What will happen if you start notepad or IE 3 times? Where 3 processes are started or 3 threads are started ?
  • How does thread synchronization occurs inside a monitor ?
  • How will you call an Applet using a Java Script function ?
  • Is there any tag in HTML to upload and download files ?
  • Why do you Canvas ?
  • How can you push data from an Applet to Servlet ?
  • What are 4 drivers available in JDBC ?
  • How you can know about drivers and database information ?
  • If you are truncated using JDBC, How can you know ..that how much data is truncated ?
  • And What situation , each of the 4 drivers used ?
  • How will you perform transaction using JDBC ?
  • In RMI, server object first loaded into the memory and then the stub reference is sent to the client ? or whether a stub reference is directly sent to the client ?
  • Suppose server object is not loaded into the memory, and the client request for it , what will happen?
  • What is serialization ?
  • Can you load the server object dynamically? If so, what are the major 3 steps involved in it ?
  • What is difference RMI registry and OSAgent ?
  • To a server method, the client wants to send a value 20, with this value exceeds to 20,. a message should be sent to the client ? What will you do for achieving for this ?
  • What are the benefits of Swing over AWT ?
  • Where the CardLayout is used ?
  • What is the Layout for ToolBar ?
  • What is the difference between Grid and GridbagLayout ?
  • How will you add panel to a Frame ?
  • What is the corresponding Layout for Card in Swing ?
  • What is light weight component ?
  • Can you run the product development on all operating systems ?
  • What is the webserver used for running the Servlets ?
  • What is Servlet API used for connecting database ?
  • What is bean ? Where it can be used ?
  • What is difference in between Java Class and Bean ?
  • Can we send object using Sockets ?
  • What is the RMI and Socket ?
  • How to communicate 2 threads each other ?
  • What are the files generated after using IDL to Java Compilet ?

Java swing Placement Papers interview questions

1) Can a class be it's own event handler? Explain how to implement this.

Answer: Sure. an example could be a class that extends Jbutton and implements ActionListener. In the actionPerformed method, put the code to perform when the button is pressed.

2) Why does JComponent have add() and remove() methods but Component does not?

Answer: because JComponent is a subclass of Container, and can contain other components and jcomponents.

3) How would you create a button with rounded edges?

Answer: there's 2 ways. The first thing is to know that a JButton's edges are drawn by a Border. so you can override the Button's paintComponent(Graphics) method and draw a circle or rounded rectangle (whatever), and turn off the border. Or you can create a custom border that draws a circle or rounded rectangle around any component and set the button's border to it.

4) If I wanted to use a SolarisUI for just a JTabbedPane, and the Metal UI for everything else, how would I do that?

Answer: in the UIDefaults table, override the entry for tabbed pane and put in the SolarisUI delegate. (I don't know it offhand, but I think it's "com.sun.ui.motiflookandfeel.MotifTabbedPaneUI" - anything simiar is a good answer.)

5) What is the difference between the 'Font' and 'FontMetrics' class?

Answer: The Font Class is used to render 'glyphs' - the characters you see on the screen. FontMetrics encapsulates information about a specific font on a specific Graphics object. (width of the characters, ascent, descent)

6) What class is at the top of the AWT event hierarchy?

Answer: java.awt.AWTEvent. if they say java.awt.Event, they haven't dealt with swing or AWT in a while.

7) Explain how to render an HTML page using only Swing.

Answer: Use a JEditorPane or JTextPane and set it with an HTMLEditorKit, then load the text into the pane.

8) How would you detect a keypress in a JComboBox?

Answer: This is a trick. most people would say 'add a KeyListener to the JComboBox' - but the right answer is 'add a KeyListener to the JComboBox's editor component.'

9) Why should the implementation of any Swing callback (like a listener) execute quickly?

A: Because callbacks are invoked by the event dispatch thread which will be blocked processing other events for as long as your method takes to execute.

10) In what context should the value of Swing components be updated directly?

A: Swing components should be updated directly only in the context of callback methods invoked from the event dispatch thread. Any other context is not thread safe?

11) Why would you use SwingUtilities.invokeAndWait or SwingUtilities.invokeLater?

A: I want to update a Swing component but I'm not in a callback. If I want the update to happen immediately (perhaps for a progress bar component) then I'd use invokeAndWait. If I don't care when the update occurs, I'd use invokeLater.

12) If your UI seems to freeze periodically, what might be a likely reason?

A: A callback implementation like ActionListener.actionPerformed or MouseListener.mouseClicked is taking a long time to execute thereby blocking the event dispatch thread from processing other UI events.

13) Which Swing methods are thread-safe?

A: The only thread-safe methods are repaint(), revalidate(), and invalidate()

14) Why won't the JVM terminate when I close all the application windows?

A: The AWT event dispatcher thread is not a daemon thread. You must explicitly call System.exit to terminate the JVM.

Placement Papers & Technical Interview Questions

Q1: What are the advantages of OOPL?

Ans: Object oriented programming languages directly represent the real life objects. The features of OOPL as inhreitance, polymorphism, encapsulation makes it powerful.

Q2: What do mean by polymorphisum, inheritance, encapsulation?

Ans: Polymorhisum: is a feature of OOPl that at run time depending upon the type of object the appropriate method is called.
Inheritance: is a feature of OOPL that represents the "is a" relationship between different objects(classes). Say in real life a manager is a employee. So in OOPL manger class is inherited from the employee class.
Encapsulation: is a feature of OOPL that is used to hide the information.

Q3: What do you mean by static methods?

Ans: By using the static method there is no need creating an object of that class to use that method. We can directly call that method on that class. For example, say class A has static function f(), then we can call f() function as A.f(). There is no need of creating an object of class A.

Q4: What do you mean by virtual methods?

Ans: virtual methods are used to use the polymorhism feature in C++. Say class A is inherited from class B. If we declare say fuction 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.

Q5: Given two tables Student(SID, Name, Course) and Level(SID, level) write the SQL statement to get the name and SID of the student who are taking course = 3 and at freshman level.

Ans: SELECT Student.name, Student.SID
FROM Student, Level
WHERE Student.SID = Level.SID
AND Level.Level = "freshman"
AND Student.Course = 3;

Q6: What are the disadvantages of using threads?

Ans: DeadLock.

 
 

Q1: Write the Java code to declare any constant (say gravitational constant) and to get its value

Ans: Class ABC
{
static final float GRAVITATIONAL_CONSTANT = 9.8;
public void getConstant()
{
system.out.println("Gravitational_Constant: " + GRAVITATIONAL_CONSTANT);
}
}
Q2: What do you mean by multiple inheritance in C++ ?

Ans: 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.

Q3: Can you write Java code for declaration of multiple inheritance in Java ?

Ans: Class C extends A implements B
{
}

C++ C Programming Puzzles

76)
#include <stdio.h>
#define MAXI 100
main(){
int x=6,done,i;
done=i=0;
do
{
if((x/=2)>1)
{i++; continue;}
else
done++;
}while ((i < MAXI) && !done);

printf("%d %d\n",i,done);
}

77)
#include <stdio.h>
main()
{
extern int i;
i=20;
printf("%d\n",sizeof(i));
}

78)
#include <stdio.h>
fun()
{
printf("Yes\n");
}

#define fun() printf("No\n")

main()
{
fun();
(fun)();
}

79)
#include <stdio.h>
main()
{
int i = 1;
switch(i) {
printf("\nHello, ");
case 1: printf("One, ");
i++;
break;
case 2: printf("Two");
break;
}
}

80)
#include <stdio.h>
#define DESHAWCURRENTDEBUGLEVEL 1
void main(void)
{
int i = 10 ;
int j = 15 ;

#ifdef DESHAWCURRENTDEBUGLEVEL
printf("%d\n",i);
#else
printf("%d\n",j);
#endif
}

81)
#include <stdio.h>
#define scanf "%s DE Shaw"
main()
{
printf(scanf,scanf);
}

82)
#include <stdio.h>
main()
{
char *p="abc";
char *q="abc123";

while(*p==*q)
{
printf("%c %c",*p,*q);
p++;q++;
}
}

83)
#include <stdio.h>
#define INTPTR int *
main()
{
INTPTR pi, pj;
int i,j;
i=10;j=20;
pi = &j;
pj = &j;
j++;
i= *pi;
printf("%d,",i);
j++;
// i= *pj;
printf("%d",pj);
}

84)
#include <stdio.h>
#include<string.h>
main()
{
char strp[] = "Never ever say no";
char *chp, c='e';
int i,j;
chp = strrchr(strp, c);
i = chp-strp;
for(j=0;j<=i;j++)printf("%c",strp[j]);
}

85)
#include <stdio.h>
main()
{
char str[] ="abcdef";
printf("str is %s",str);
str = "DESIS";
printf("str is %s",str);
}

86)
#include <stdio.h>
main()
{
int i = 10;
printf(" %d %d %d \n", ++i, i++, ++i);
}

87)
#include <stdio.h>
#include<string.h>
main()
{
char *str ="India pvt. ltd.";
char *str1 = "DESIS";
printf("str is %s",str);
printf("str is %s",str1);
strcpy(str,str1);
printf("str is %s",str);
}

88)
#include <stdio.h>
#include<string.h>
main()
{
char str[] ="DESIS India pvt. ltd.";
const char *str1= str;
strcpy(str1,"DESHAW");
printf("str is %s",str);
}

89)
#include <stdio.h>
main()
{
int i=4,j=2,k=0;
char c1='a',c2='b';
if(k==0)printf("k is zero\n");
else if(j==2)printf("j is 2\n");
else if(i==4)printf("i is 4\n");
if(c1!='a')printf("c1 is not a\n");
else if (c2=='a')printf("c2 is b");
else printf("Hello\n");
}

90)
#include <stdio.h>
main()
{
int a[3] = {1,2,3};
int i= 2;
printf("\n %d %d\n", a[i], i[a]);
}

91)
#include <stdio.h>
void fun(int, int*);
main()
{
int j,i;
int * intptr;
printf("enter an integer\n");
scanf("%d",&i);
intptr = &j;
j = i;
printf("i and j are %d %d \n",i,j);
fun(j,intptr);
printf("i is:%d",i);
printf("\n j is:%d",j);
}
void fun(int k, int *iptr)
{
k++;
(*iptr)++;
return;
}

92)
#include <stdio.h>
main()
{
int x;
x = printf("%d\n",x=printf("%d\n",100));
printf("%d\n",x);
}

93)
#include <stdio.h>
main()
{
int i;
char c;
for (i=0;i<5;i++){
scanf("%d",&c);
printf("%d",i);
}
}

94)
#include <stdio.h>
main()
{
int x = 10,y=2,z;
z=x/*y+y*/+y;
printf("%d\n",z);
}

95)
#include <stdio.h>
main()
{
int a[] = {0,1,2,3,4};
int *p[] = {a,a+1,a+2,a+3,a+4};
int **pp = p;

printf("%d, %d, %d ", *pp-a, pp-p, **pp);
pp++; pp++;;++pp;*++pp;
printf("%d, %d, %d ", pp-p, *pp-a, **pp);
}

96)
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
main()
{
int *p, *c, i;
i = 5;
p = malloc(sizeof(i));
printf("\n%d",*p);
*p = 10;
printf("\n%d %d",i,*p);
c = calloc(2,i);
printf("\n%d\n",*c);
}

97)
#include <stdio.h>
main()
{
char input[] = "SSSWILTECH1\1\1";
int i, c;
for ( i=2; (c=input[i])!='\0'; i++)
{
switch(c)
{
case 'a': putchar ('i'); continue;
case '1': break;
case 1: while (( c = input[++i]) != '\1' && c!= '\0');
case 9: putchar('S');
case 'E': case 'L': continue;
default: putchar(c);continue;
}
putchar(' ');
}
putchar('\n');
}

98)
#include <stdio.h>
main()
{
unsigned int k = 987 , i = 0;
char trans[10];

do
{
trans[i++] = (k%16 > 9) ? (k%16 - 10 + 'a') : (k%16 - '0' );

} while(k /= 16);

for(i=0;i<10;i++)
printf("%c", trans[i]);
}

99)
#include <stdio.h>

main()
{
unsigned int k = 987 , i = 0;
char trans[10];

do {
trans[i++] = (k%16 > 9 ? k%16 - 10 + 'a' : k%16 - '0' );
printf("%d %d\n",k,k%16);

} while(k /= 16);

printf("%s\n", trans);
}

100)
#include <stdio.h>
main()
{
char *pk;
const char* p;
const char c = 'a';
char c1='b';
p=&c1;
pk = &c;
printf("%c %c",*pk,*p);
}

C++ C Programming Puzzles

101)
#include <stdio.h>
main()
{

int i=4;

if (i>5) printf("Hi");

else f(i);
}

f(
int j)
{

if (j>=4) f(j-1);

else
if(j==0)return;
printf(
"Hi");

return;
}

102)
#include <stdio.h>
int *NEXT(register
int i)
{

int *ipt;
ipt = &i;
ipt++;

return ipt;
}

main ()
{

int j;
printf(
"%d",(NEXT(j)));
}

103)
#include <stdio.h>
#define PRINT(int) printf("int = %d ",int)
main()
{

int x,y,z;
x=
03;y=02;z=01;
PRINT(x^x);
z<<=
3;PRINT(x);
y>>=
3;PRINT(y);
}

104)
#include <stdio.h>
#define PRINT(int) printf( "int = %d ", int)
main()
{

int x=03,y=02,z=01;
PRINT (x | y & ~z);
PRINT (x & y && z);
PRINT (x ^ y & ~z);
}

105)
#include <stdio.h>
main()
{

int p;

for(p = 1; p<=10,--p; p=p+2)
puts(
"Hello");
}

106)
#include <stdio.h>
int n, R;
main()
{
R =
0;
scanf(
"%d",&n);
printf(
"\n
%d, %d",fun(n),R);
}

int fun(int n)
{

if (n>3) return R = 5;
R =
6;

return(1);
}

107)
#include <stdio.h>
main()
{

int arr[3][3] = {1,2,3,4,5,6,7,8,9};

int i,j;

for (j=2;j>=0;j--){

for(i=2;i>=0;i--){
printf(
"\n%d",*(*(arr+i)+j));
printf(
"\n TATATATA");
}
}
}

108)
#include <stdio.h>
main()
{

int a = 10, b = 5,c = 3,d = 3;


if ((a<b) && (c = d++))
printf(
" %d
%d
%d
%d ", a, b, c, d);

else
printf(
" %d
%d
%d
%d ", a, b, c, d);

}

109)
#include <stdio.h>
main()
{

struct test
{

char c;

int i;

char m;
} t1;
printf(
"%d
%d\n",sizeof(t1), sizeof(t1.c));
}

110)
#include <stdio.h>
main()
{

int a,b;
scanf(
"%d
%d", &a, &b);
printf(
"%d\n", a+++b);
printf(
"%d
%d\n",a,b);
}

111)
#include <stdio.h>
int i;
main()
{

char a[] = "Shiva";
printf(
"%c\n",i[a]);
}

112)
#include <stdio.h>
myread(a,b)
{
printf(
"%d
%d",a,b);
}

main()
{
myread(
2,4);
}

113)
#include <stdio.h>
funct(
char* str)
{
printf(
"%s\n",str);
}

main()
{

static
int ii = 1;

int jj = 5;
ii+=++jj;
funct(ii+++
"Campus Interview");
}

114)
#include <stdio.h>
funct(str)
{
printf(
"%s\n",str);
}

main()
{
funct(
'-'-'-'+"DEShaw");
}

115)
#include <stdio.h>
main()
{
printf(
" %d\n",'-'-'-'+'/'/'/');
}

116)
#include <stdio.h>
static
int a = 6;
extern
int a;

main()
{
printf(
"%d",a);
}

117)
#include <stdio.h>
main()
{

int i=6,j=4;
printf(
"NO\n");

switch(i)
{

do{

case
1: printf("yes\n");


case
2:


case
3:


case
4:


case
5:


case
6:
j--;
}
while (j);
}
}

118)
#include <stdio.h>
abc(
int *i, int *j)
{
*i = *i + *j;
*j = *i - *j;
*i = *i - *j;
}
main()
{

int i = 5, j=10;
abc(&i,&j);
printf(
"%d..%d",i,j);
}

119)
#include<stdio.h>
int main()
{

char c;
c=
255;
printf(
"%d",c);

return(0);
}

120)
#include<stdio.h>
main()
{

int x=5,p=10;
printf(
"%*d",x,p);
}

121)
#include<stdio.h>
main()
{

int a[]={2,3,4,5,6};

int i=0;
printf(
"%d",a[i++]+i[a+1]);
}

122)
#include<stdio.h>
#include<stdlib.h>
main()
{

int *ptr=(int*)malloc(sizeof(int));
*ptr=
4;
printf(
"%d",*ptr++);
}

123)
#include<stdio.h>
main()
{

float x=3.14;
printf(
"%e, %E, %f",x,x,x);
}

124)
#include<stdio.h>
int main(int k)
{

if(k<10)
printf(
"%d ",main(k+1));

return k;
}

125)
#include<stdio.h>
main()
{

int i=4;
printf(
"%d
%d
%d",++i,i++,i*i);
printf(
"\n");
printf(
"%d
%d
%d",i*i,++i,i++);
}

C++ C Programming Puzzles

126)
#include<stdio.h>
main()
{
char c='6';
int d=6;
printf("%d %d %d",d,d+=c>'0'&&c<='7',c++);
}

127)
#include<stdio.h>
main()
{
int a=4,b=2;
a=b<<a + b>>2;
b=b<<a + b>>2;
printf("%d %d",a,b);
}

128)
#include<stdio.h>
main()
{
int i=98765;
printf("%d\n",printf("%d",printf("%d",i)));
}

129)
#include<stdio.h>
main()
{
char c;
while(c=getchar()!='a')printf("%d",c);
}
//INPUT=dcba

130)
#include<stdio.h>
main()
{
int *i=0;
printf(" %p\n",i);
}

131)
#include<stdio.h>
#include<stdlib.h>
main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
}

132)
#include<stdio.h>
unsigned getbits(unsigned a,int i,int j)
{
return(a>>(i+1-j)) & ~(~0<<j);
}
main()
{
unsigned num=128;
printf("%d\n",getbits(num,7,5));
}

133)
#include<stdio.h>
#define sq(x) x*x
main()
{
int a=5;
printf("%d\n",sq(a+5));
}

134)
#include<stdio.h>
#define concatinate(a,b) a##b
#define same1(a) #a
#define same2(a) same1(a)
main()
{
printf("%s\n",same2(concatinate(1,2)));
printf("%s\n",same1(concatinate(1,2)));
}

135)
#include<stdio.h>
main()
{
int a=1,b=3,c,d;
c=(a,b);
d=a,b,c;
printf("%d %d",c,d);
}

136)
#include<stdio.h>
#include<stdlib.h>
main()
{
int*ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
}

137)
#include<stdio.h>
main()
{
int x;
x=20;
printf("x:%d\n",x);
printf("sizeof(x++) is: %d\n",sizeof(x++));
printf("x:%d\n",x);
}

138)
#include <stdio.h>
#define f(x,y) x##y
#define g(x) #x
#define h(x) g(y)

int main()
{
printf("%s\n",h(f(2,3)));
printf("%s\n",g(f(2,3)));
return 0;
}

139)
#include <stdio.h>
int main()
{
int i;
i = 10,20,30;
printf("i:%d\n",i);
}

140)
#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int Shiftfn(int a)
{
int t;
t = a<<2 + a;
return t;
}

int main()
{
int i = 1, j = 2,k = 3;
PrintInt(Shiftfn(i));
PrintInt(Shiftfn(j));
PrintInt(Shiftfn(k));
}

141)
#include<stdio.h>
enum {false,true};

int main()
{
int i=1;
do
{
printf("%d\n",i);
i++;
if(i < 15)
continue;
}while(false);
}

142)
#include <stdio.h>
int main()
{
float a = 12.5;
printf("%d\n",a);
printf("%d\n", *(int *)&a);
return 0;
}

143)
#include<stdio.h>
int main()
{
int a=1;
switch(a)
{ int b=20;
case 1: printf("b is %d\n",b);
break;
default:printf("b is %d\n",b);
break;
}
return 0;
}

144)
#include<stdio.h>
#include<string.h>
int s(char*A[20],char*B[20])
{
char *a,*b;
a=A,b=B;
while(*a++!=*b++);
*a=*b='\0';
return strlen(A);
}

int main()
{
char A[20]="somestring",B[20]="debugthecbug";
printf("%d %s %s\n",s(&A,&B),A,B);
return 0;
}

145)
#include<stdio.h>
void insert(char a[],int n)
{
int i,j;
for(i=j=0;a[i]!='\0';i++)
if(a[i]!=n)
a[j++]=a[i];
a[j]='\0';
}
main()
{
char a[]="helloworld";
insert(a,'l');
printf("%s",a);
}

146)
#include<stdio.h>
#include<stdlib.h>
void weird(int*a)
{
a=(int*)malloc(sizeof(int));
}
main()
{
int*a;
weird(a);
*a=6;
printf("%d\n",*a);
}

147)
#include <stdio.h>
main()
{
int a=4,b=10;
printf("%d %d %d %d\n",a,a^=b=b^=a=a^=b,b,printf("%d %d %d\n",
b,a,a^=b=b^=a=a^=b));
}

148)
#include<stdio.h>
int fun1()
{
static int c=20;
return --c;
}

int fun2()
{
static int c=1;
return fun1()+c--;
}

int main()
{
int i=0;
while(i<fun2())
printf("%d ",i++);
return 0;
}

149)
#include<stdio.h>
#include<string.h>
main()
{
char a[]="aaa";
char *b="bbb";
strcpy(a,"cc");
printf("%s",a);
strcpy(b,"dd");
printf("%s",b);
}

150)
#include<stdio.h>
int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))

int main()
{
printf("%d",SIZE);
if(-1<=SIZE) printf("1");
else printf("2");
return 0;
}

C++ C Interview Question Papers

36)
What is the output of the following program?
#include<stdio.h>
int fun1()
{
static int c=20;
return --c;
}

int fun2()
{
static int c=1;
return fun1()+c--;
}

int main()
{
int i=0;
while(i<fun2())
printf("%d ",i++);
return 0;
}
Ans:



37)
What is the output of the following program?
#include<stdio.h>
#include<string.h>
void p(char *a)
{
static int y=1;
if(y=1-y) printf("%c",*a);
return;
}

int main()
{
char *a;a=(char*)malloc(20*sizeof(char));
strcpy(a,"DbugtheCbug");
while(*a!='\0') p(a++);
printf("\n");
return 0;
}
Ans:



38)
Give the output of the following piece of code.
#include <stdio.h>
main()
{
int a=4,b=10;


printf("%d %d %d %d\n",a,a^=b=b^=a=a^=b,b,printf("%d %d %d\n",b,a,a^=b=b^=a=a^=b));


}
Ans:




39)
Predict the output of the following program.
#include<stdio.h>
int main()
{
int x = 5,p = 10;
printf("%*d",x,p);
}
Ans:




40)
What is the output of the following code?
#include<stdio.h>
int main()
{
int i=2;
i=i++;
printf("%d ",i++ + i++);
}
Ans:




41)
Predict the output for the below code.
#include<stdio.h>
int main()
{
int a[]={2,3,4,5,6};
int i=0;
printf("%d",a[i++]+i[a+1]);
return(0);
}
Ans:



42)
What is the output of the following piece of code?
#include<stdio.h>
#include<stdlib.h>
void weird(int *a)
{
a=(int*)malloc(sizeof(int));
}
int main()
{
int *a;
weird(a);
*a=6;
printf("%d\n",*a);
return(0);
}
Ans:



43)
What is the output of the following piece of code?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",*ptr++);
return(0);
}
Ans:




44)
Correct the following code in case of any errors.
#include <stdlib.h>
#include <stdio.h>
#define SIZE 15
int main()
{
int *a, i;
a = (void*)malloc(SIZE*sizeof(int));
for (i=0; i<SIZE; i++)
*(a + i) = (int)i * i;
for (i=0; i<SIZE; i++)
printf("%d\n", *a++);
free(a);
return 0;
}
Ans:




45)
What is the output of the following program?
#include<stdio.h>
int main()
{
char c;
c=255;
printf("%d",c);
return(0);
}

C C++ Interview Question Papers

16)
What is the output of the following code for the input "dcba"?
#include<stdio.h>
#include<stdlib.h>
int main()
{
char c;
while (c=getchar()!='a') printf("%d",c);
return 0;
}
Ans:



17)
What is the output of the following code?
#include<stdio.h>
int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))

int main()
{
printf("%d",SIZE);
if(-1<=SIZE) printf("1");
else printf("2");
return 0;
}
Ans:



18)
What is the output of the following code?
#include<stdio.h>
#include<string.h>
int main()
{
char a[]="aaa";
char *b="bbb";
strcpy(a,"cc");
printf("%s",a);
strcpy(b,"dd");
printf("%s",b);
return 0;
}
Ans:




19)
Can you predict the output of the following code snippet ?
#include<stdio.h>
#define sq(x) x*x
int main()
{
int a=3;
printf("%d\n",sq(a+3));
}
Ans:



20)
What is the output of this code?
#include<stdio.h>
int main()
{
int *i=0;
printf(" %p\n",i);
return(0);
}
Ans:




21)
Does the following C code compile ???? If it does .. can you
explain what happens when you run it with some arbitrary text
as input or what does it do ????
[ The input is redirected from a file called "input.txt" ]
int main()
{
for(;scanf("%*[^a-z]")+1;putchar(getchar()));
}
Ans:




22)
What is the output of the following code?
#include<stdio.h>
int main()
{
int a=1,b=3,c,d;
c=(a,b);
d=a,b,c;
printf("%d %d",c,d);
return 0;
}
Ans:




23)
What is the expected output of the code ?????
#include<stdio.h>
#define concatinate(a,b) a##b
#define same1(a) #a
#define same2(a) same1(a)
int main()
{
printf("%s\n",same2(concatinate(1,2)));
printf("%s\n",same1(concatinate(1,2)));
return 0;
}
Ans:



24)
The intention of the following program was to print 42 astericks('*')
But it fails to do so.
You have to add/replace/delete exactly one character in the program
to make it work ???
Find as many solutions as possible.

#include <stdio.h>
int main()
{
int n = 42;
for(int i = 0; i < n; i-- )
printf("*");
return 0;
}
Ans:























25)
What does this code do?
#include<stdio.h>
void insert(char s[],int c)
{
int i,j;
for(i=j=0;s[i]!='\0';i++)
if(s[i]!=c)
s[j++]= s[i];
s[j]='\0';
}

int main()
{
char s[]="helloworld";
insert(s,'l');
printf("%s",s);
return(0);
}
Ans:




26)
What is the output of the following code?
#include<stdio.h>
#include<string.h>
int s(char *A[20],char *B[20])
{
char *a,*b;
a=A;b=B;
while(*a++!=*b++); *a=*b='\0';
return strlen(A);
}
int main()
{
char A[20]="somestring",
B[20]="debugthecbug";
printf("%d %s %s\n",s(&A,&B),A,B);
return 0;
}
Ans:




27)
Can you predict the output ?
#include<stdio.h>
#define print(var) printf("%s : %d\n",#var,(var))
int main()
{
int y = 100;
int *p;
p = new int;
*p = 10;
y = y/*p; /*dividing y by *p */;
print(y);
return 0;
}
Ans:



28)
Take the input as 4, and write the output of the following code.
#include <stdio.h>
main()
{
int i=0;
printf("%d %d\n",scanf(" %d",&i),i);
printf("%d %d\n",i=4,i);
}
Ans:



29)
Predict the output of the following program.
#include <stdio.h>
int main()
{


int a=3, b = 5;
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c\n%c%c\n%c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);


return 0;
}
Ans:





30)
What is the output of the following code?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
return(0);
}
Ans:




31)
What is the output of the following piece of code?
#include<stdio.h>
int main(int k)
{
if(k<10)
printf("%d ",main(k+1));
return k;
}
Ans:





32)
Predict the output or error if any in the following code.
#include<stdio.h>
main()
{
int i = 4;
printf("%d %d %d",++i,i++,i*i);
printf("\n");
printf("%d %d %d",i*i,++i,i++);
system("PAUSE");
}
Ans:



33)
Give the output of the following piece of code.
#include<stdio.h>
int main()
{
int a,b,c=2,d=10;


printf("%d\n ",scanf("%d%d%d",&a,&b,&c,&d) ) ;


printf("%d\n%d\n%d\n%d\n",a,b,c);
return(0);
}
Ans:



34)
#include <stdio.h>
main()
{
int a[15][10][5]={1};
func(a);
}
Which of these is/are correct function prototypes for func ?

void func( int a[][10][5] );
void func( int a[15][][5] );
void func( int a[15][10][] );
Ans:





35)
Give the output of the following piece of code.
#include <stdio.h>
main()
{
float x=3.14;
printf("%e, %E, %f",x,x,x);
}
Ans:

Top C++ C Interview Questions

1)
Find the bug in this code.
#include<stdio.h>
int main()
{
int i=3,j=5;
printf("%d",(i+j)++);
return(0);
}
Ans:



2)
What is the output of the following piece of code?
#include<stdio.h>
int main()
{
char c='8';
int d=8;
printf("%d %d %d",d,d+=c>='0'&&c<='9',c++);
return(0);
}
Ans:



3)
x=x*y+1; --------------1
Is this different from x=x*(y+1); --------------2

How to right the assignment operation "2" with out using
parenthesis in one statement.
Ans:




4)
What does this code do?
#include<stdio.h>
void func(char s[],int c)
{
int i,j;
for(i=j=0;s[i]!='\0';i++)
if(s[i]!=c)
s[j++]= s[i] + s[i]==c ;
s[j]='\0';
}

int main()
{
char s[]="aelloworld";
func(s,'k');
printf("%s",s);
return(0);
}
Ans:



5)
Can you find any difference between the two scanf calls made in the
code given below ?

#include<stdio.h>
int main()
{
int x,y;
scanf("%d",&x);
scanf(" %d",&y);
return 0;
}
Ans:




6)
Predict the output of the following piece of code.
Assume that bit position 0 is at the right end and that i and j are
sensible positive values.
#include<stdio.h>
unsigned getbits(unsigned a,int i,int j)
{
return(a>>(i+1-j)) & ~(~0<<j);
}
int main()
{
unsigned num=128;
printf("%d\n",getbits(num,7,5));
return(0);
}
Ans:



7)
What does this code do on input 'abcd' from console?
#include<stdio.h>
int main()
{
char c;
while(c=getchar()!='\n')
printf("%d",c);
return(0);
}
Ans:


8)
What does this code do?
#include<stdio.h>
int main()
{
int i;
int a[20]={1,2,3,4,5,6,7,8,9,10,10,9,8,7,6,1,2,3,4,5};
int n=20;
for(i=0;i<20;i++)
{
printf("%6d%c",a[i],(i%10==9 || i== 19)?'\n':' ');
}
return(0);
}
Ans:



9)
Will this code work properly? If yes why? If not why not?
#include<stdio.h>
void swap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
int main()
{
int a=3617283450;
int b=3617283449;
swap(&a,&b);
printf("%d %d\n",a,b);
return(0);
}
Ans:



10)
What is the output of the following piece of code?
#include<stdio.h>
int main()
{
int a=3,b=1;
a = b<<a + b>>2;
b = b<<a + b>>2;
printf("%d %d",a,b);
return 0;
}
Ans:



11)
Can you predict the output of the following code snippet ?
#include<stdio.h>
int main()
{
int i=9876;


printf("%d\n",printf("%d",printf("%d",i)));


return 0;
}
Ans:




12)
Give the output of the following code.
#include<stdio.h>
typedef struct s
{
int a[10];
}s;
typedef struct t
{
int *a;
}t;

int main()
{
s s1,s2;t t1;
s1.a[0]=0;

t1.a=s1.a;
s1.a[0]++;
s2=s1;
s1.a[0]++;

printf("%d %d %d",s1.a[0],s2.a[0],t1.a[0]);
return 0;
}
Ans:



13)
You just have to write the declaration of an unsigned int p;
Obviously you can't use any spaces or new lines in the declaration !!
Ans:




14)
What is the output of the following code?
#include<stdio.h>
int main()
{
int x=2,y=3,z1,z2;
z1=x + + + y;
z2=x+++y;
printf("%d %d %d %d",x,y,z1,z2);
return 0;
}
Ans:



15)
Predict the output of the following code with the following inputs ->
1. 2c3
2. 2 5
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *i=0,*j=4;
i=(int*)malloc(sizeof(int));
scanf("%dc%d",&i,&j);
printf("%d %d",i,j);
return(0);
}
Ans:

programming questions/puzzles

The following is a simple C program to read a date and print the date. Run it and explain the behaviour


#include <stdio.h>


int
main()
{

int
day,month,year;

printf("Enter the date (dd-mm-yyyy) format including -'s:");

scanf("%d-%d-%d",&day,&month,&year);

printf("The date you have entered is %d-%d-%d\n",day,month,year);

return
0;
}


 

The following is a simple C program to read and print an integer. But it is not working properly. What is(are) the mistake(s)?


#include <stdio.h>


int
main()
{

int
n;

printf("Enter a number:\n");

scanf("%d\n",n);


printf("You entered %d
\n",n);

return
0;
}


 

The following is a simple C program which tries to multiply an integer by 5 using the bitwise operations. But it doesn't do so. Explain the reason for the wrong behaviour of the program.


#include <stdio.h>


#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))


int
FiveTimes(int
a)
{

int
t;

t
=
a<<2
+
a;

return
t;
}


int
main()
{

int
a
=
1, b
=
2,c
=
3;

PrintInt(FiveTimes(a));

PrintInt(FiveTimes(b));

PrintInt(FiveTimes(c));

return
0;
}


 

Is the following a valid C program?


#include <stdio.h>


#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))


int
max(int
x, int
y)
{
(x
>
y) ? return x :
return
y;
}


int
main()
{

int
a
=
10, b
=
20;

PrintInt(a);

PrintInt(b);

PrintInt(max(a,b));
}


 

The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work.


#include <stdio.h>


int
main()
{

int
i;

int
n
=
20;

for( i
=
0; i
<
n; i-- )

printf("-");

return
0;
}

Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three.


 

What's the mistake in the following code?


#include <stdio.h>


int
main()
{

int*
ptr1,ptr2;

ptr1
=
malloc(sizeof(int));

ptr2
=
ptr1;

*ptr2
=
10;

return
0;
}


 

What is the output of the following program?


#include <stdio.h>


int
main()
{

int
cnt
=
5, a;


do {

a
/=
cnt;
} while (cnt
--);


printf ("%d\n", a);

return
0;
}


 

What is the output of the following program?


#include <stdio.h>


int
main()
{

int
i
=
6;

if( ((++i
<
7) && ( i++/6)) || (++i
<=
9))
;

printf("%d\n",i);

return
0;
}


 

What is the bug in the following program?


#include <stdlib.h>


#include <stdio.h>


#define SIZE
15


int
main()
{

int
*a, i;


a
=
malloc(SIZE*sizeof(int));


for (i=0; i<SIZE; i++)

*(a
+
i) =
i
*
i;

for (i=0; i<SIZE; i++)

printf("%d\n", *a++);

free(a);

return
0;
}


 

Is the following a valid C program? If so, what is the output of it?


#include <stdio.h>


int
main()
{

int
a=3, b
=
5;


printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);

printf(&a["WHAT%c%c%c
%c%c
%c !\n"], 1["this"],

2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);

return
0;
}


 

What is the output of the following, if the input provided is:
Life is beautiful


#include <stdio.h>


int
main()
{

char
dummy[80];

printf("Enter a string:\n");

scanf("%[^a]",dummy);

printf("%s\n",dummy);

return
0;
}


 

Note : This question has more to do with Linker than C language

We have three files a.c, b.c and main.c respectively as follows:
a.c
---

int
a;

b.c
---

int
a
=
10;

main.c
------

extern
int
a;

int
main()

{


printf("a = %d\n",a);


return
0;

}

Let's see what happens, when the files are compiled together:

bash$ gcc a.c b.c main.c

bash$ ./a.out

a = 10

Hmm!! no compilation/linker error!!! Why is it so??


 

The following is the offset macros which is used many a times. Figure out what is it trying to do and what is the advantage of using it.


#define offsetof(a,b) ((int)(&(((a*)(0))->b)))


 

The following is the macro implementation of the famous, Triple xor swap.


#define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))

What are the potential problems with the above macro?


 

What is the use of the following macro?


#define DPRINTF(x) printf("%s:%d\n",#x,x)


 

Let's say you were asked to code a function IAddOverFlow which takes three parameters, pointer to an integer where the result is to be stored, and the two integers which needs to be added. It returns 0 if there is an overflow and 1 otherwise:


int
IAddOverFlow(int*
result,int
a,int
b)
{

/* ... */

}

So, how do you code the above function? (To put in a nutshell, what is the logic you use for overflow detection?)


 

What does the following macro do?


#define ROUNDUP(x,n) ((x+n-1)&(~(n-1)))


 

Most of the C programming books, give the following example for the definition of macros.


#define isupper(c) (((c) >=
'A') && ((c) <=
'Z'))

But there would be a serious problem with the above definition of macro, if it is used as follows (what is the problem??)


char
c;

/* ... */


if(isupper(c++))
{

/* ... */

}

But most of the libraries implement the isupper (declared in ctypes.h) as a macro (without any side effects). Find out how isupper() is implemented on your system.


 

I hope you know that ellipsis (...) is used to specify variable number of arguments to a function. (What is the function prototype declaration for printf?) What is wrong with the following delcaration?


int
VarArguments(...)
{

/*....*/


return
0;
}


 

Write a C program to find the smallest of three integers, without using any of the comparision operators.


 

What does the format specifier %n of printf function do?


 

Write a C function which does the addition of two integers without using the '+' operator. You can use only the bitwise operators.(Remember the good old method of implementing the full-adder circuit using the or, and, xor gates....)


 

How do you print I can print % using the printf function? (Remember % is used as a format specifier!!!)


 

What's the difference between the following two C statements?


const
char
*p;

char*
const
p;


 

What is the difference between memcpy and memmove?


 

What is the format specifiers for printf to print double and float values?


 

Write a small C program to determine whether a machine's type is little-endian or big-endian.


 

Write a C program which prints Hello World! without using a semicolon!!!