IBM Top JSP interview questions
IBM Top C interview questions
When should the register modifier be used? Does it really help?
IBM Test Paper
Technical part is easy we can finish in 20 mins. Aptitude was tough out of nearly 100 members, they shorlisted 35 candidates for interview process. I appiled thru some ad only i think, i didn't remember properly thru how i appiled for this.I am listing some questions which i remember. Tech part no need of preparation only, just like that u can do. 1. In 1930 a man's age is the last 2 digit of the year intercganged in which he born and his granpa's age also like the same. 2.10pow10pow10 if one takes 1 sec to write a single digit then how long will it take to write the above _expression. 3. 2 trains coming in opposite directions in 12 km/hr distance between the trains is 24 km. an insect starts at the same time when the train starts at a speed of 18km/hr. How many km the insect might have travelled before it got crashed between the 2 trains. 4. less than 30 of them are there. Among those 1/3rd know french 1/4th knows english, 1/2 knows german, then 1/8 of them donn't know any of the languages. Then find out how many are there totally. 5. what will be the unit digit number od 7 pow 173 6. what is the probability that 2 of them selected randomly have got the same month of birth? 7. in c output of one prg. Very easy 8. parity bit for what? 9. if in a graph some nodes are there ....can we tell it is bipartite.. (question on discrete) 10-14. One comprehension and 5 questions based on that. 15. some 3 pairs of white socks, 4 pairs of red socks and 2 pairs of black socks among that what is the probability that correct pairs can be taken out? 16. YWCC is what in options they had specified for compiler... a big hats off to chetana mam and suri sir for maintaining such a wonderful group just to serve people who are in search of job. Really thru this group we are getting the chance to know the current openings..
IBM PLACEMENT PAPERS NEW
1) I give a jcl code with Programmer name: smith Jobname:ABC Jobclass=c Message class = a All messages are to be displaced code it in a JCL. A) //ABC,smith,class=c,message class=a,message level=(2,1) B) //ABC,smith,class=c,message class=a,message level =(!,1) C) //ABC,smith,class=c,message class=a,message level=(0,1) D) none of these. 2) for a cobol program Move 5 to work-sd. A statement which says that the value of work-st is 0. perform Para1. …………… Para1. Add 3 to work-sd Add 2 to work-st If work-st <11 go to main-para. ……………… go to para1. Wat wud b the value of work-sd? a) 15 b) 18 c) 10 d) none 3) given 2 tables T1 and T2. T1 T2 A 1 B 2 3 u make a query SELECT * FROM T1,T2. Wat is the no: of rows that are fetched frm this query? a) 4 b) 5 c) 6 d) 9 4) wat is the full form of RACF? 5) who is the data base manager of db2? a) buffer register b) runtime suprivisor c) DBRM d) None 5)u have a file with DISP=SHR, when u open the file in output mode a) the records get appended b) the records gets overwritten c) the job abbends d) none of these 6) u open a file with disp= MOD, when u open the file in output mode (same options as the above question) 7) to exclude some records while sorting which parameter do we use? a) OMIT b) EXCLUDE c) ….d)…. 8)when u insert into db2 with cics, it works as a) foreground b) a job working in background c) .. d) ….. 9)u have a dataset TIME.WORK.OCT88, u want to concatenate other datasets like TIME.WORK.JUL88, TIME.WORK.APR88, TIME.WORK.MAY88. how do u do that? a)//DDname DD DSN = TIME.WORK.OCT88, DISP = SHR DD DSN = TIME.WORK.JUL88, DISP = SHR DD DSN = TIME.WORK.APR88, DISP = SHR DD DSN = TIME.WORK.MAY88, DISP = SHR b)//DDname DD DSN = TIME.WORK.JUL88, DISP = SHR DD DSN = TIME.WORK.OCT88, DISP = SHR DD DSN = TIME.WORK.APR88, DISP = SHR DD DSN = TIME.WORK.MAY88, DISP = SHR c)//DDname DD DSN = TIME.WORK.APR88, DISP = SHR DD DSN = TIME.WORK.JUL88, DISP = SHR DD DSN = TIME.WORK.MAY88, DISP = SHR DD DSN = TIME.WORK.OCT88, DISP = SHR e) none of these 10) when db2 entries are made while working in cics, the enteries should be made in a) TCT b) PCT c) DCT d) RCT 11) find the odd one out a) KSDS b) RRDS c) LDS d) PDS 12) full form of VTOC 13) question based on SKIP and COUNT parameter
IBM PLACEMENTS JAVA
Question: Name the containers which uses Border Layout as their default layout? Question: Is Iterator a Class or Interface? What is its use? Similarities: Question: How to define an Abstract class? Question: How to define an Interface? Question: Explain the user defined Exceptions? Question: Explain the new Features of JDBC 2.0 Core API? Question: Explain garbage collection? Question: How you can force the garbage collection? Question: What is OOPS? Question: Describe the principles of OOPS. Question: Explain the Encapsulation principle. Question: Explain the Inheritance principle. Question: Explain the Polymorphism principle. Question: Explain the different forms of Polymorphism. Question: What are Access Specifiers available in Java? Question: Describe the wrapper classes in Java. Following table lists the primitive types and the corresponding wrapper classes: Primitive Wrapper boolean java.lang.Boolean byte java.lang.Byte char java.lang.Character double java.lang.Double float java.lang.Float int java.lang.Integer long java.lang.Long short java.lang.Short void java.lang.Void Question: Read the following program: public class test { What is the result? Question: What is the difference between the instanceof and getclass, these two are same or not ?
Answer: Containers which uses Border Layout as their default are: window, Frame and Dialog classes.
Question: What do you understand by Synchronization?
Answer: Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. Synchronization prevents such type of data corruption.
E.g. Synchronizing a function:
public synchronized void Method1 () {
// Appropriate method-related code.
}
E.g. Synchronizing a block of code inside a function:
public myFunction (){
synchronized (this) {
// Synchronized code here.
}
}
Question: What is Collection API?
Answer: The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.
Answer: Iterator is an interface which is used to step through the elements of a Collection.
Question: What is similarities/difference between an Abstract class and Interface?
Answer: Differences are as follows:
Answer: A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Example of Abstract class:
abstract class testAbstractClass {
protected String myString;
public String getMyString() {
return myString;
}
public abstract string anyAbstractFunction();
}
Answer: In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.
Emaple of Interface:
public interface sampleInterface {
public void functionOne();
public long CONSTANT_ONE = 1000;
}
Answer: User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.
Example:
class myCustomException extends Exception {
// The class simply has to exist to be an exception
}
Answer: The JDBC 2.0 API includes the complete JDBC API, which includes both core and Optional Package API, and provides inductrial-strength database computing capabilities.
New Features in JDBC 2.0 Core API:
Answer: Garbage collection is one of the most important feature of Java. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program cann't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. I Java on calling System.gc() and Runtime.gc(),
JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.
Answer: Garbage collection automatic process and can't be forced.
Answer: OOP is the common abbreviation for Object-Oriented Programming.
Answer: There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.
Answer: Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
Answer: Inheritance is the process by which one object acquires the properties of another object.
Answer: The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as "one interface, multiple methods".
Answer: From a practical programming viewpoint, polymorphism exists in three distinct forms in Java:
Answer: Access specifiers are keywords that determines the type of access to the member of a class. These are:
Answer: Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
public static void main(String [] args) {
int x = 3;
int y = 1;
if (x = y)
System.out.println("Not equal");
else
System.out.println("Equal");
}
}
A. The output is "Equal"
B. The output in "Not Equal"
C. An error at " if (x = y)" causes compilation to fall.
D. The program executes but no output is show on console.
Answer: C
Question: what is the class variables ?
Answer: When we create a number of objects of the same class, then each object will share a common copy of variables. That means that there is only one copy per class, no matter how many objects are created from it. Class variables or static variables are declared with the static keyword in a class, but mind it that it should be declared outside outside a class. These variables are stored in static memory. Class variables are mostly used for constants, variable that never change its initial value. Static variables are always called by the class name. This variable is created when the program starts i.e. it is created before the instance is created of class by using new operator and gets destroyed when the programs stops. The scope of the class variable is same a instance variable. The class variable can be defined anywhere at class level with the keyword static. It initial value is same as instance variable. When the class variable is defined as int then it's initial value is by default zero, when declared boolean its default value is false and null for object references. Class variables are associated with the class, rather than with any object.
Answer: instanceof is a operator, not a function while getClass is a method of java.lang.Object class. Consider a condition where we use
if(o.getClass().getName().equals("java.lang.Math")){ }
This method only checks if the classname we have passed is equal to java.lang.Math. The class java.lang.Math is loaded by the bootstrap ClassLoader. This class is an abstract class.This class loader is responsible for loading classes. Every Class object contains a reference to the ClassLoader that defines. getClass() method returns the runtime class of an object. It fetches the java instance of the given fully qualified type name. The code we have written is not necessary, because we should not compare getClass.getName(). The reason behind it is that if the two different class loaders load the same class but for the JVM, it will consider both classes as different classes so, we can't compare their names. It can only gives the implementing class but can't compare a interface, but instanceof operator can.
The instanceof operator compares an object to a specified type. We can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface. We should try to use instanceof operator in place of getClass() method. Remember instanceof opeator and getClass are not same. Try this example, it will help you to better understand the difference between the two.
Interface one{
}
Class Two implements one {
}
Class Three implements one {
}
public class Test {
public static void main(String args[]) {
one test1 = new Two();
one test2 = new Three();
System.out.println(test1 instanceof one); //true
System.out.println(test2 instanceof one); //true
System.out.println(Test.getClass().equals(test2.getClass())); //false
}
}
IBM Placements For Freshers
Posted: Tue, 27 Nov 2007 08:20:24 -0600 Question:Computing the exact number of ways that N things can be taken M at a time can be a great challenge when N and/or M become very large.So given 5<=N<=100 5<=M<=100 and M<=N.Compute the EXACT value of: N!/(M!*(N-M)!) The final value of C will fit in a 32-bit Pascal LongInt or a C long. So deduce an approach based on this assumption given. sol*=(long)pow((double)V[i],P[N][i]-P[R][i] Solutions to Logical Puzzles-2 1)3 Posted: Tue, 27 Nov 2007 09:54:40 -0600 Question.All the positive numbers can be expressed as a sum of one, two or more consecutive positive integers. For example 9 can be expressed in three such ways, 2+3+4, 4+5 or 9. Given an integer ,you will have to determine in how many ways that number can be expressed as summation of consecutive numbers.
Solution:At a mere glance,this problem may seem to have a direct solution,though it isn't upon a long thought.The naive approach to calculate combination of N things taken M at a time is obtained by mere application of formula,which mean finding N! ,M! and (N-M)! and the putting them in the expression.But as N and M are independently large enough for overflows to occur in N! and M! calculations.
One of the approaches to solve this problem,based on prime factorization can be formulated in the following manner.
Consider a prime P whose exponents in N!,M! and (N-M)! are p1,p2 and p3 respectively.
therefore the exponent of P in N!/M!*(N-M)! is p1+p2-p3.
This way we can store the exponents of all primes <= the largest prime which divides N.
One this is done ,we can merely multiply all these primes each raised to its exponent to get the required answer.
Now getting on to the tricky part of finding the exponent of a prime P in N!
In general ,if P is any prime,then the no of P's in N! is given by
Sum (floor(n/P^i)) for P^i <= N This apparently weird formula can be explained with ease!! consider the numbers 1,2,3,.........,,N.Every Pth number is divisible by P.So all those multiples of P put together contribute floor(N/P) to the power of P. Similarly every P^2th number is divisible by P^2.And each of them have already contributed 1 to power of P in previous iteration,as each of them are divisible by P.So each of them contribute an additional 1, which means they all together contribute floor(N/P^2) to the already sum. These terms of contribution go on till we have P^i <= N,hence the summation Sum (floor(N/P^i)) for P^i <= N The Cpp implementation of above task is given below
#include<iostream>
using namespace std;
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<vector>
typedef vector<int> powers;
void insert(vector<powers> &P,int i,vector<int> V,int size)
{
vector<int> temp=P[i-1];
//calculation of exponent of each prime
for(int j=0;j <size && i!=1;j++)
{
while(i%V[j]==0)
{
temp[j]++;
i/=V[j];
}
}
P.push_back(temp);
}
int main()
{
vector<int> V;
vector<powers> P;
V.push_back(2);
bool flag;
int N,R;
for(int i=3;i<100;i+=2)
{
flag=true;
for(int j=0;V[j]*V[j] <=i;j++)
{
if(i%V[j]==0)
{
flag=false;
break;
}
}
if(flag)
{
V.push_back(i);
}
}
int size=V.size();
vector<int> temp(size,0);
P.push_back(temp);
P.push_back(temp);
for(int i=2;i<=100;i++)
{
insert(P,i,V,size);
}
while(cin >> N >>R)
{
if(N==0 && R==0)
{
return(0);
}
else
{
long sol=1;
for(int i=0;i<size;i++)
{
-P[N-R][i]); //Pi^(p1-p2-p3)
}
cout<<N<<" things taken
"<<R<<" at a time is
"<<sol<<" exactly."<<'\n';
}
}
return(0);
}
It is of the form 0^2+2,1^2+2,3^2+2,6^2+2,10^2+2,.....
The sequence of n is 0,1,3,6,10,... so the next n is 15 => 15^2+2
2)2
The function is S(n)=S(n-1)*(n+2) + 3 ,n>1
=6 ,n=1
3)1
It is of the form S(n)*3+2 for even and S(n)*2-3 for odd where S(n) is the nth element.
So 7th element = 239*2-3=475
4)2
The function is S(n)=(n+2)^3 -2
5)1
The numbers in the sequence is a combination of n^2 and n^3 and n is odd.
6)2
The function is S(n)=S(n-1)*n-n n>1
=3 n=1
7)
8)1
The function is S(n)=(S(n-1)-1)*(n-1) n>1
=8 n=1
9)
10)
Solution:
2+3+4=(1+2+3+4)-(1)
=(1+2+3+4+5)-(1+2+3)
=(1+2+3+4+5+6+7+8+9)-(1+2+3+4+5+6+7+8)
This shows that the no of solutions to this problem is the no of tuples (n,m) such that
given number K can be expressed as K=(1+2+..........+n)-(1+2+3+...........+m)
and n > m
i.e K=n*(n+1)/2 -m*(m+1)/2
=(n-m)(n+m+1)/2
2*k=(n-m)*(n+m+1)
If we put the factor n-m=p and (n+m+1)=q,we get S=2*k=p*q
As p+q=2*n+1 is odd and S is even either n-m is odd otherwise n+m+1 is odd.
This leaves out with finding out the no of odd divisors of S,which can be easily done using prime factorization.
if S=(2^p)*(3^q)*(5^r)*(7^s)......
then the solution is (q+1)*(r+1)*(s+1)*.. i.e the no
IBM PLACEMENTS PAPERS LATEST
2)3, 1)To check for it, create two pointers,and set each to the start of the list. Update each as follows:
Basic Questions
* What is a friend, and why do you need it?
* If this doesn't compile, why didn't it (or will it compile?) Don't show comments, which explain the problem.
template void HashTable <>::dummy()
{
K* k = NULL;
Hashable* h = k; // If this fails to compile, it's because
// K is not derived from Hashable.
}
* This will loop forever. Why? Will it really loop forever? (Answer: Base:func() does not call Base::func(). Base: is just a label, so the line always will call Derived::func() until it runs out of stack space)
class Base {
public:
Base() {}
virtual void func() { /* do something */ }
};
class Derived : public Base {
public:
Derived() {}
virtual void func()
{
Base:func();
/* do something else */
}
};
main()
{
Derived d;
d.func(); // Never returns!
}
More advanced questions:
* What is a vtbl ?
* What is RTTI and why do you need it?
* How do I specialize a template? Give an example.
To separate sheep from goats (for those claiming C++ Guru status):
* What is a partial template? Why would you use one?
* How to I create a binary functor in the STL?
Given the following code:
class A;
class B;
class C {
A* a_;
B* b_;
public:
};
Implement a copy constructor and assignment operator for C. A sample solution is something like:
class C {
A* a_;
B* b_;
void swap(C& rhs) { rhs.a_ = a_; rhs.b_ = b_; }
public:
C(const C& rhs) {
auto_ptr<> a(new A(rhs.a_));
auto_ptr<> b(new B(rhs.b_)):
delete a_;
delete b_;
a_ = a.release();
b_ = b.release();
}
C& operator=(const C& rhs) {
C temp(rhs);
temp.swap(*this);
return *this;
}
};
What is wrong with this class, assuming that this is its complete interface?
class C {
char *p;
public:
C() { p = new char[64]; strcpy(p, "Hello world"); }
~C() { delete p; }
void foo() { cout << "My ptr is: '" << p << "'" << endl; }
};
Since this has an overtly programmed destructor, the member wise semantics for destruction are not good enough; therefore, they are not good enough for copy and assignment either. But, the copy ctor and op= are not programmed, so we will have some serious trouble.
Gradual hinting: what happens when we make a copy? [correct answer: pointer is copied]. Now, the original goes out of scope, what happens to the copy? [pointer dangles]. How would you fix it?
[also, that delete p should be delete[ p since p was allocated with the array new]
Assuming that swap() and copy construction are part of your interface for class C, what's the cookie-cutter pattern for operator= that uses them?
answer:
C& C::operator=(const C &rhs) {
if (this != &rhs) {
C tmp(rhs);
this->swap(tmp);
}
return *this;
}
Solutions to Logical Puzzles-3
The number obtained by dividing 48 with 4 is 12 which is even while all the others get odd number for the same.
3)4,
The sequence has to be AON,EWR,IEV,MMZ
4)2
The remaining 3 are input devices.
5)2
The remaining 3 represent an image.
6)3
Trousers can only take a plural form while others can also take a singular form.
7)
8)
9)4
All the other three lie inside a cell.
10)
11)4
12)3
13)1
14)2
15)3
16)3
17)1
18)3
19)3
20)4
We need 3 cuts to cut a cube into 6 equal pieces,with one cut in each dimension.Maximum identical pieces obtained with n cuts[if n is a factor of 3] = (n/3 + 1)^3, with n/3 cuts, we form n/3 + 1 equal pieces.If we need to get maximum number of identical cubes with some number of cuts then the number of cuts in all the dimensions should be equal if possible or almost equal.
The number of identical pieces formed with l,n,m cuts in the 3 demensions are (l+1)*(n+1)*(m+1).
1)3,Here there are 6,7,8 cuts in each dimension
2)2
3)4,Here the number of cuts are 7,7,6 in the 3 dimensions.
4)1
5)3
All the problems from 6-18 are based mostly on imagination.Note that 27 identical pieces have no color at all i.e they are inner pieces.
6)3
7)4
8)1
9)3
10)2
11)2
12)4
13)1
14)4
15)3
16)1
17)4
18)1
19)2
20)4
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular linked list\n");
}
}
2)Union x : 1101791232 21.500000
Union y : 100 d 0.000000
3)It is an object of some class whose purpose is to indicate that a real object of that class does not exist. One common use for a null object is a return value from a member function that is supposed to return an object with some specified properties but cannot find such an object.
4) Java,Smalltalk,Eiffel,Sather.
5)A container class is a class that is used to hold objects in memory or
external storage. A container class acts as a generic holder. A
container class has a predefined behavior and a well-known interface. A
container class is a supporting class whose purpose is to hide the
topology used for maintaining the list of objects in memory. When a
container class contains a group of mixed objects, the container is
called a heterogeneous container; when the container is holding a group
of objects that are all the same, the container is called a homogeneous
container.
6)fffffff0
7)C was the C++ predecessor. As it's name implies, a lot of C remains in C++. Although not actually being more powerful than C, C++ allows the programmer to more easily manage and operate with Objects, using an OOP (Object Oriented Programming) concept.
C++ allows the programmer to create classes, which are somewhat similar to C structures. However, to a class can be assigned methods, functions associated to it, of various prototypes, which can access and operate within the class, somewhat like C functions often operate on a supplied handler pointer.
Although it is possible to implement anything which C++ could implement in C, C++ aids to standarize a way in which objects are created and managed, whereas the C programmer who implements the same system has a lot of liberty on how to actually implement the internals, and style among programmers will vary a lot on the design choices made.
In C, some will prefer the handler-type, where a main function initializes a handler, and that handler can be supplied to other functions of the library as an object to operate on/through. Others will even want to have that handler link all the related function pointers within it which then must be called using a convention closer to C++.
To finish this discussion, C++ applications are generally slower at runtime, and are much slower to compile than C programs. The low-level infrastructure for C++ binary execution is also larger. For these reasons C is always commonly used even if C++ has alot of popularity, and will probably continue to be used in projects where size and speed are primary concerns, and portable code still required (assembly would be unsuitable then).
8)Incomplete types
refers to pointers in which there is non availability of the
implementation of the referenced location or it points to some location
whose value is not available for modification.
int *i=0x400 // i points to address 400
*i=0; //set the value of memory location pointed by i.
9)Printf : Call by value
Scanf : Call by reference
10)a const pointer means the pointer which represents the address of one value. so if you declare a pointer inside the function, it doesn't have scope outside the function. if it is also available to the outside function whenever we declare a pointer as const.
11)Type casting must be done wheneever the data type of the variable to which u r gonna assign some values is diff from the data type of the variable on the right side.
for instance;
float f;
int i = 10 , j = 5 ;
f = (float) ( i / j ) ;
f -------> left side variable.
i -------> right side variable.
but always make sure that the size of the var on the left is greater than that of the right. else there will be data loss.
A type cast should not be used to override a const or volatile declaration. Overriding these type modifiers can cause the program to fail to run correctly.
A type cast should not be used to turn a pointer to one type of structure or data type into another. In the
rare events in which this action is beneficial, using a union to hold the values makes the programmer.s
intentions clearer.
12)he answer is the standard library function qsort(). It.s the easiest sort by far for several reasons:
It is already written.
It is already debugged.
It has been optimized as much as possible (usually).
Void qsort(void *buf, size_t num, size_t size, int (*comp)(const void *ele1, const void *ele2));
13)The answer depends on what you mean by quickest. For most sorting problems, it just doesn't matter how quick the sort is because it is done infrequently or other operations take significantly more time anyway. Even in cases in which sorting speed is of the essence, there is no one answer. It depends on not only the size and nature of the data, but also the likely order. No algorithm is best in all cases.
There are three sorting methods in this author.s .toolbox. that are all very fast and that are useful in different situations. Those methods are quick sort, merge sort, and radix sort.
The Quick Sort
The quick sort algorithm is of the .divide and conquer. type. That means it works by reducing a sorting
problem into several easier sorting problems and solving each of them. A .dividing. value is chosen from the input data, and the data is partitioned into three sets: elements that belong before the dividing value, the value itself, and elements that come after the dividing value. The partitioning is performed by exchanging elements that are in the first set but belong in the third with elements that are in the third set but belong in the first Elements that are equal to the dividing element can be put in any of the three sets.the algorithm will still work properly.
The Merge Sort
The merge sort is a .divide and conquer. sort as well. It works by considering the data to be sorted as a
sequence of already-sorted lists (in the worst case, each list is one element long). Adjacent sorted lists are merged into larger sorted lists until there is a single sorted list containing all the elements. The merge sort is good at sorting lists and other data structures that are not in arrays, and it can be used to sort things that don.t fit into memory. It also can be implemented as a stable sort.
The Radix Sort
The radix sort takes a list of integers and puts each element on a smaller list, depending on the value of its least significant byte. Then the small lists are concatenated, and the process is repeated for each more significant byte until the list is sorted. The radix sort is simpler to implement on fixed-length data such as ints.
14)Both the merge sort and the radix sort are good sorting algorithms to use for linked lists.
15)The preprocessor is used to modify your program according to the preprocessor directives in your source code. Preprocessor directives (such as #define) give the preprocessor specific instructions on how to modify your source code. The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments. If your source code contains any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly.
The C preprocessor is used to modify your program according to the preprocessor directives in your source code. A preprocessor directive is a statement (such as #define) that gives the preprocessor specific instructions on how to modify your source code. The preprocessor is invoked as the first part of your compiler program.s compilation step. It is usually hidden from the programmer because it is run automatically by the compiler.
16)The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.
The following functions can be used to convert strings to numbers:
Function Name Purpose
atof() Converts a string to a double-precision floating-point value.
atoi() Converts a string to an integer.
atol() Converts a string to a long integer.
strtod() Converts a string to a double-precision floating-point value and reports any .leftover. numbers that could not be converted.
strtol() Converts a string to a long integer and reports any .leftover. numbers that could not be converted.
strtoul() Converts a string to an unsigned long integer and reports any .leftover. numbers that could not be converted.
17)
The standard C library provides several functions for converting numbers of all formats (integers, longs, floats, and so on) to strings and vice versa
The following functions can be used to convert integers to strings:
Function Name Purpose
itoa() Converts an integer value to a string.
ltoa() Converts a long integer value to a string.
ultoa() Converts an unsigned long integer value to a string.
The following functions can be used to convert floating-point values to strings:
Function Name Purpose
ecvt() Converts a double-precision floating-point value to a string without an embedded decimal point.
fcvt() Same as ecvt(), but forces the precision to a specified number of digits.
gcvt() Converts a double-precision floating-point value to a string with an embedded decimal point.
18)The heap is where malloc(), calloc(), and realloc() get memory.
Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap
is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order. Such
memory isn't deallocated automatically; you have to call free().
Recursive data structures are almost always implemented with memory from the heap. Strings often come
from there too, especially strings that could be very long at runtime. If you can keep data in a local variable (and allocate it from the stack), your code will run faster than if you put the data on the heap. Sometimes you can use a better algorithm if you use the heap.faster, or more robust, or more flexible. It's a tradeoff.
If memory is allocated from the heap, it.s available until the program ends. That's great if you remember to deallocate it when you.re done.
19)n++ takes more than one instruction, ++n is faster. n++ has to store n, increment the variable and return n, while ++n increment n and return without storing the previous value of n.
20) If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.
21)expression if (a=0) always return false
expression if (a=1) always return true
22)Malloc is dynamic memory allocation,it allocates the memory and initialize garbage value.Calloc is similar to malloc but only difference is initialize zero
23)A middle level language
24)Overloading is polymorphism which is one of the characteristics of Object oriented programming. C is not and object oriented language like C++ or Java. Therefore, no overloading, inheritance, etc.
25)Static int variable are accessed only inside the file where it is defined. Thus we can have same variable name in 2 files if the variable is defined as static. The scope of the variable is limited to the file in which it is defined.
On the other hand if the variable is not defined as static and defined globally then it can be accessed across the files. To access the variable which is global variable and declared and defined in file A, keyword "extern" is used in front of the variable in file B. This indicated to compiler while compiling that the variable is defined in some other file other than B and continues compiling and while linking the variable it search for the actual definition and links.
IBM PLACEMENTS C PAPERS
C program to delete a tree Write a C program to delete a tree(i.e, free up its nodes) #include<stdio.h> Question:Write a C program to determine the number of elements(or size) in a binary tree? chtree Hi Friends,
struct binarysearchtree
{
int data;
struct binarysearchtree* left;
struct binarysearchtree* right;
};
typedef struct binarysearchtree* tree;
void tree_free(tree T)
{
if (T==NULL)
return;
else
{
tree_free(T->left);
tree_free(T->right);
free(T);
}
}
Solution:
#include<stdio.h>
struct binarysearchtree
{
int data;
struct binarysearchtree* left;
struct binarysearchtree* right;
};
typedef struct binarysearchtree* tree;
int tree_size(tree T)
{
if(T==NULL)
return 0;
else
{
return 1+tree_size(T->left)+tree_size(T->right);
}
}
{
int data;
struct binarysearchtree* left;
struct binarysearchtree* right;
};
typedef struct binarysearchtree* tree;
int max(int a,int b)
{
if(a >=b)
return a;
else
return b;
}
int height(tree T)
{
if(T==NULL)
return 0;
else
{
int h1=height(T->left);
int h2=height(T->right);
return 1+max(h1,h2);
}
}
One of my friend who is working for amazon gave me a good collection of questions which helped him to get into Amazon. I am sharing these questions to help you guys.
* Constructor?
* Destructor?
* Default constructor?
* Copy constructor?
* Conversion constructor?
* member function as virtual?
* member function as static?
* member variable as static?
* destructor as static?
std::auto_ptr ptr(new char[10]);