Sunday, May 18, 2008

Technical Interview Questions Exception Handling in Constructors

Technical Interview Questions Exception Handling in Constructors

Constructors do not have return type and so they cannot return error codes. How are errors or exceptions handled in constructors? What if the calls that you make in the constructor can actually throw exceptions? How do you let the caller know something bad happened in a constructor?

There are a few ways to do robust error/exception handling in constructors

1. Do as little in the constructor has you can. Then provide an Init() function in the constructor, which does the normal initialization stuff. The user can then call this function after creating an object. The problem here is, its up to the user to actually call the Init() function. The user could potentially miss this step, making this method error prone. However, there are a lot of places where this methodology is used. You are trying to eliminate error handling in the constructor by using this method
2. Another way to do this is by putting the object in a Zombie state. This is one approach you can take especially if you do not have the option of using exceptions. When you go with this option, you will also to do provide a function that will check the state of the object after construction. The downsides to this option is that, its up to the user to do these checks and the users will need to do this every time one attempts to create an object. It's usually always better and cleaner to throw an exception instead. Use the Zombie option as a last resort.
3. The downsides to the above methods can be reduced by making the constructor private or protected, expose a CreateInstance() public method, and do all the error handling here rather than leave it to the user. But sometimes, its not possible to handle all the error conditions in a generic manner and you will need to throw an exception.
4. If an exception is thrown in the constructor, the destructor will not get called. So you need to handle and clean up as much as you can before you leave the constructor. The best way to do this is using the "resource allocation is initialization" technique. I will cover this topic separately in a future post. But the basic idea is to assign resource allocation and cleanup to other objects. Basically, you are trying to get allocation out of the way (indirect) so that you don't have to do it explicitly. When you don't allocate something directly, you don't have to release it either because it will be done by the component or class who deals with it. E.g. If you need to allocate some memory or open up a file, You can use smart objects (smart pointer, auto_ptr, smart file handlers etc..) instead of calling new or fopen directly. When you do this, and if an exception is thrown in your constructor, the smart objects will automatically release the resources it acquired, as the stack unwinds. If you do not use the "resource allocation is initialization" technique, the user will need to wrap the statements in try/catch block and rethrow after cleaning up the mess, something like what the finally block does in Java or C#. Although this works in theory, it's up to the user to make this work and it also always a source of errors and bugs (esp. memory and handle leaks) and is messy

As you have seen, there is no "one size fits all" rule to do error/exception handling in constructors. I have listed the most commonly used methods and one of these should work most of the time.

Technical questions with answers, explanations c c++ java

Technical questions with answers, explanations c c++ java

void swap(int &i, int &j)

{

int temp = i;

i = j;

j = temp;

}

Instead of writing a separate function for each data type, you could write a MACRO or templatize the function.

Swapping without using a temporary variable is an age old trick and there are a few ways to do this. You could use of the basic arithmetic operations like +,-,/,*

1: void swap(int &i, int &j)

2: {

3: i=i+j;

4: j=i-j;

5: i=i-j;

6: }

The technique involves storing the sum of variables in one of them and then extracting it back by subtracting the other number. There are different variants to this technique. E.g, instead of starting by storing the sum, you could store the difference, product or the quotient. The last two could lead you to round-off and integer division errors. However all of them have one fundamental flaw. Its in line 3, and the issue is that this could lead to an overflow error.

This is another technique the gets you around these issues; the XOR Swapping technique

void swap(int &i, int &j)

{

i = i ^ j;

j = j ^ i;

i = i ^ j;

}

This is an elegant technique and should work well with any primitive data type and you could write a simple MACRO like






define SWAP(i, j) (((i) ^= (j)), ((j) ^= (i)), ((i) ^= (j)))







Although, the XOR technique gets rid of the other issues like overflow and round off errors that we encountered in the previous technique, the lands in into yet another issues; This does not work when you try to swap the same memory location. However if can get around this by a simple 'if' check or a more elegant OR check like






define SWAP(i, j) ( (i==j) || ((i) ^= (j)), ((j) ^= (i)), ((i) ^= (j)))







The first OR condition (i == j) is checked before the actual SWAP. (You do not need a SWAP if the both memory locations hold the same data) without additional space

Technical questions with answers, explantions

Technical questions with answers, explantions

A normal search across a BST (Binary Search Tree) would look like this

bool BinaryTree::Search (int data )

{

Node *current = this->root;







while ( current != NULL )

{

if (current->data < data)

{

current = current->left;

}

else if (current->data > data)

{

current = current->right;

}

else if ( current->data == data )

{

return true;

}

}







return false;

}

You keep going down the tree, until you find a node whose value is equal to one you are looking for, or you bail out when you hit a leaf (NULL) node. If you look at the number of statements, there is one conditional check on the while, and an average of 1.5 conditional checks inside the loop. That makes it a total of 2.5 checks every iteration. On a tree with a 1000 nodes, that's 2500 checks.

Let's see how we can improve this. I am using the sentinel node technique for this purpose. In this case static Node * Leaf;

This is how the search will look like




static Node* Leaf;







bool BinaryTree::Search (int data )

{

Node *current = this->root;







Leaf->data = data;




while ( current->data != lead->data )

{

if (current->data < data)

{

current = current->left;

}

else

{

current = current->right;

}

}




return (current != Leaf);







}

The sentinel is a static node, and while building the tree, you point all the leaf nodes to this sentinel node instead of NULL. Before you start the search, you set the value of the sentinel node to the data you are searching for. This way you are guaranteed to get a hit. You just need to do one extra check at the end to see if the Hit node was a Node in the tree or the sentinel node. If you look at the number of conditional statements in the loop, there is one in the while statement and one inside the loop, that makes it 2 searches every iteration. You just saved half a conditional statement. Don't underestimate this improvement. E.g. In a 1000 iteration loop you saved 500 checks.

This is extremely useful with large trees or when you are searching the tree several times or any other scenario where this call happens in a hot section.

Satyam GENERAL APTITUDE ANTONYMS

Satyam GENERAL APTITUDE ANTONYMS

Directions:Each question given below consists of a word, followed by four words or phrases. Choose the lettered word or phrase that is most nearly opposite in meaning to the word in the question.

ANTONYMS

1. Disregarded
(a) heed
(b) hopeful
(c) evade
(d) dense

Ans. (a)


2. Obviate
(a) becloud
(b) necessitate
(c) rationalize
(d) execute

Ans. (b)


3. Superficial
(a) profound
(b) exaggerated
(c) subjective
(d) spirited

Ans. (a)


4. Abide
(a) retract an offer
(b) refuse to endure
(c) shield from harm
(d) exonerate

Ans. (b)


5. Acerbity
(a) noteworthiness
(b) hypocrisy
(c) mildness of temperament
(d) lack of anxiety

Ans. (c)



Directions: Each question or group of questions is based on a passage or set of conditions. For each question, select the best answer choice given.

Quesitions 6-9

In a certain society, there are two marriage groups, Red and Brown. No marriage is permitted within a group. On marriage, males become part of their wife's group: women remain in their own group. Children belong to the same group as their parents. Widowers and divorced males revert to the group of their birth. Marriage to more than one person at the same time and marriage to a direct descendant are forbidden.

6. A Brown female could have had
I. a grandfather born Red
II. a grandmother born Red
III. two grandfathers born Brown

(a) I only
(b) II only
(c) I and II only
(d) II and III only
(e) I,II and III

Ans. (c)


7. A male born into the Brown group may have

(a) an uncle in either group
(b) a Brown daughter
(c) a Brown son
(d) a son-in-law born into the Red group
(e) a daughter-in-law in the Red group

Ans. (a)


8. Which of the following is not permitted under the rules stated?

(a) A Brown male marrying his father's sister
(b) A Red female marrying her mother's brother
(c) A man born Red, who is now a widower, marrying his brother's widow
(d) A widower marrying his wife's sister
(e) A widow marrying her divorced daughter's ex-husband

Ans. (b)


9. If widowers and divorced males retained the group they had upon marrying, which of the following would have been permissible?(Assume no previous marriages occurred)

(a) A woman marrying her dead sister's husband
(b) A woman marrying her divorced daughter's ex-husband.
(c) A widower marrying his brother's daughter
(d) A woman marrying her mother's brother, who is a widower
(e) A divorced male marrying his ex-wife's divorced sister

Ans. (d)

Satyam APTITUDE Placement Papers Freshers

Satyam APTITUDE Placement Papers Freshers

Questions 10-13

Tom wishes to enroll in Latin AA, Sanskrit A, Armenian Literature 221, and Celtic Literature 701.
Latin AA meets five days a week, either from 9 to 11 A.M or from 2 to 4 P.M.
Sanskrit A meets either Tuesday and Thursday from 12 noon to 3 P.M., or Monday, Wednesday, and Friday
from 10 A.M to 12 noon.
Armenian Literature 221 meets either Monday, Wednesday, and Friday from 12:30 to 2 P.M., or Tuesday and Thursday
from 10:30 A.M to 12:30 P.M
Celtic Literature 701 meets by arrangement with the instructor, the only requirement being that it meet for one four-hour session or two two-hour sessions per week, between 9A.M and 4 P.M from Monday to Friday, beginning on the hour.


10. Which combination is impossible for Tom?

(a) Latin in the morning, Sanskrit on Tuesday and Thursday, and Armenian Literature on Monday, Wednesday, Friday
(b) Latin in the afternoon and Sanskrit and Armenian Literature on Monday, Wednesday, and Friday.
(c) Latin in the afternoon, Sanskrit on Monday, Wednesday, and Friday,and Armenian Literature on Tuesday and Thursday
(d) Latin in the morning and Sanskrit and Armenian Literature on Monday, Wednesday, and Friday
(e) Latin in the afternoon, Armenian Literature on Monday, Wednesda and Friday, and Celtic Literature on Tuesday

Ans. (d)


11. Which of the following gives the greatest number of alternatives for scheduling Celtic Literature, assuming that all other courses

(a) Latin in the afternoon and Armenian Literature Monday, Wednesday and Friday
(b) Sanskrit on Tuesday and Thursday and Armenian Literature on Monday, Wednesday and Friday
(c) Latin in the afternoon and Armenian Literature Tuesday and Thursday
(d) Latin in the morning and Sanskrit on Tuesday and Thursday
(e) Sanskrit on Monday, Wednesday, and Friday. and Armenian Literature on Tuesday and Thursday

Ans. (a)


12. If the Celtic instructor insists on holding at least one session on Friday, in which of the following can Tom enroll?
(I) Armenian Literature on Monday, Wednesday, and Friday
(II) Sanskrit on Monday, Wednesday, and Friday

(a) I only
(b) II only
(c) both I and II
(d) I or II but not both
(e) neither I nor II

Ans. (d)


13. Which of the following additional courses, meeting as indicated, can Tom take?

(a) Maths--Monday, Wednesday, and Friday from 10A.M to 12 noon
(b) French--Monday, Wednesday, and Friday from 11A.M to 12:30 P.M
(c) English--Tuesday and Thursday from 2 to 4 P.M
(d) Japenese--Tuesday and Thursday from 1 to 3 P.M
(e) Old Norse-Icelandic--Monday only from 12 to 3 P.M

Ans. (b)



Questions 14-18

(1) Ashland is north of East Liverpool and west of Coshocton
(2) Bowling Green is north of Ashland and west of Fredericktown
(3) Dover is south and east of Ashland
(4) East Liverpool is north of Fredricktown and east of Dover
(5) Fredricktown is north of Dover and west of Ashland
(6) Coshocton is south of Fredricktown and west of Dover


14. Which of the towns mentioned is furthest to the northwest ?

(a) Ashland
(b) Bowling Green
(c) Coshocton
(d) East Liverpool
(e) Fredericktown

Ans. (b)


15. Which of the following must be both north and east of Fredricktown?
(I) Ashland
(II) Coshocton
(III) East Liverpool

(a) I only
(b) II only
(c) III only
(d) I and II
(e) I and III

Ans. (e)


16. Which of the following towns must be situated both south and west of at least one other town?

(a) Ashland only
(b) Ashland and Fredricktown
(c) Dover and Fredricktown
(d) Dover,Coshocton and Fredricktown
(e) Dover,Coshocton and East Liverpool

Ans. (d)


17. Which of the following statements, if true, would make the information in the numbered statements more specific?

(a) Coshocton is north of Dover
(b) East Liverpool is north of Dover
(c) Ashland is east of Bowling Green
(d) Coshocton is east of Fredericktown
(e) Bowling Green is north of Fredericktown

Ans. (a)


18. Which of the numbered statements gives information that can be deduced from one or more of the other statement?

(a) (1)
(b) (2)
(c) (3)
(d) (4)
(e) (6)

Ans. (c)



Questions 19-22

Spelunkers International offers exploring tours in eight caves: Abbott, Benny, Caeser, Dangerfield, Ewell, Fields, Guinness, and Hope
(1) Class 1 spelunkers may not attempt cave Ewell, Fields or Hope
(2) Class 2 spelunkers may not attempt Hope
(3) Class 3 spelunkers may attempt any cave
(4) Cave Caesar may be attempted only by spelunkers who have previously explored cave Benny
(5) Cave Fields may be attempted only by spelunkers who have previously explored cave Ewell
(6) Only two of caves Benny, Caeser, Ewell, Fields, and Hope may be attempted by any explorer in a single tour


19. A class 2 spelunker who has previously explored cave Ewell may be restricted in choosing a tour by which rule(s)?
(I) Rule(4)
(II) Rule(5)
(III) Rule(6)

(a) I only
(b) II only
(c) I and III only
(d) II and III only
(e) I, II and III

Ans. (c)


20. In how many different ways may a class 1 spelunker who has never explored any of the eightcaves before set up a tour of three caves, if she wishes to explore caves Abbott and Caesar?

(a) 2
(b) 3
(c) 4
(d) 5
(e) 6

Ans. (b)

Latest Satyam Aptitude Papers For Freshers

Latest Satyam Aptitude Papers For Freshers

20. In how many different ways may a class 1 spelunker who has never explored any of the eightcaves before set up a tour of three caves, if she wishes to explore caves Abbott and Caesar?

(a) 2
(b) 3
(c) 4
(d) 5
(e) 6

Ans. (b)


21. What is the maximum number of caves that a class 3 spelunker who has previously explored only cave Benny may include
in a single tour?

(a) 4
(b) 5
(c) 6
(d) 7
(e) 8

Ans. (b)


22. If x + y = 3 and y/x= 2 then y = ?

(a) 0
(b) 1/2
(c) 1
(d) 3/2
(e) 2

Ans. (e)


23. How many squares with sides 1/2 inch long are needed to cover a rectangle that is 4 ft long and 6 ft wide

(a) 24
(b) 96
(c) 3456
(d) 13824
(e) 14266


24. If a=2/3b , b=2/3c, and c=2/3d what part of d is b/

(a) 8/27
(b) 4/9
(c) 2/3
(d) 75%
(e) 4/3

Ans. (b)


25. Successive discounts of 20% and 15% are equal to a single discount of

(a) 30%
(b) 32%
(c) 34%
(d) 35%
(e) 36

Ans. (b)


26. The petrol tank of an automobile can hold g liters.If a liters was removed when the tank was full, what part of the full tank was removed?

(a)g-a
(b)g/a
(c) a/g
(d) (g-a)/a
(e) (g-a)/g

Ans. (c)


27.If x/y=4 and y is not '0' what % of x is 2x-y

(a)150%
(b)175%
(c)200%
(d)250%

Ans. (b)


28.If 2x-y=4 then 6x-3y=?

(a)15
(b)12
(c)18
(d)10

Ans. (b)


29.Ifx=y=2z and xyz=256 then what is the value of x?

(a)12
(b)8
(c)16
(d)6

Ans. (b)


30. (1/10)18 - (1/10)20 = ?

(a) 99/1020
(b) 99/10
(c) 0.9
(d) none of these

Ans. (a)


31. Pipe A can fill in 20 minutes and Pipe B in 30 mins and Pipe C can empty the same in 40 mins.If all of them work together, find the time taken to fill the tank

(a) 17 1/7 mins
(b) 20 mins
(c) 8 mins
(d) none of these

Ans. (a)


32. Thirty men take 20 days to complete a job working 9 hours a day.How many hour a day should 40 men work to complete the job?

(a) 8 hrs
(b) 7 1/2 hrs
(c) 7 hrs
(d) 9 hrs

Ans. (b)


33. Find the smallest number in a GP whose sum is 38 and product 1728

(a) 12
(b) 20
(c) 8
(d) none of these

Ans. (c)


34. A boat travels 20 kms upstream in 6 hrs and 18 kms downstream in 4 hrs.Find the speed of the boat in still water and the speed of the water current?

(a) 1/2 kmph
(b) 7/12 kmph
(c) 5 kmph
(d) none of these

Ans. (b)


35. A goat is tied to one corner of a square plot of side 12m by a rope 7m long.Find the area it can graze?

(a) 38.5 sq.m
(b) 155 sq.m
(c) 144 sq.m
(d) 19.25 sq.m

Ans. (a)


SOME QUESTIONS WHEREIN TWO STATEMENTS ARE GIVEN ARE ALSO THERE WHERE YOU HAVE TO TELL WHICH STATEMENT IS CORRECT
SOME QUESTIONS ALSO APPEARED FROM THE BARRON'S GMAT GUIDE.
PAGE NO. 439 PASSAGE AND QUESTIONS 1 TO 9
PAGE NO. 440-441
PAGE 442 PASSAGE 2
ALSO REFER TO BARRON'S GRE BOOK FOR ADDITIONAL ANALYTICAL QUESTIONS.


PAPER 2--GENERAL AWARENESS

1. Who is the father of computers
2. Expand HTML,DMA,FAT,LAN,WAN,FDDetc
3. Which was intel's first microprocessor
4. Convert 1024 (in decimal) to octa and hexadecimal form
5. First microprocessor was
(a) 8085
(b) 8088
(c) 8086
(d) 80487

6. Give the name of a processor produced by mortorola?
7. What is the full form of WindowsNT ?
8. What is the difference between 8087 and 8086

CTS Cognizant Technology Solutions Aptitude Papers

CTS Cognizant Technology Solutions Aptitude Papers

1. Using the digits 1,5,2,8 four digit numbers are formed and the sum of all possible such numbers.

ans:106656

2. Four persons can cross a bridge in 3,7,13,17 minutes. Only two can cross at a time. find the minimum time taken by the four to cross the bridge.

ans:20


3. Find the product of the prime numbers between 1-20

ans..9699690

4. 2,3,6,7--- using these numbers form the possible four digit numbers that are divisible by 4. ans.----8

5. Two trains are traveling at 18kmph and are 60 km apart. There is fly in the train. it flies at 80kmph. It flies and hits the second train and then it starts to oscillate between the two trains. At one instance when the two trains collide it dies. Distance traveled by the fly when both trains collide is Ans.---12km

6. there are 1000 doors that are of the open-close type. When a person opens the door he closes it and then opens the other. When the first person goes he opens-closes the doors ion the multiples of 1 i.e., he opens and closes all the doors. when the second goes he opens and closes the doors 2, 4 6 8 respectively. Similarly when the third one goes he does this for 3 6 9 1 2 15th doors resly. Find number of doors that are open at last.

Ans:square numbers

7.There are 9 balls of this one is defective. Find the minimum no. of chances of finding the defective one.Ans 3times

8. There are coins of Rs.5, 2,1,50p,25p,10p,5p. Each one has got a weight. Rs 5 coin weighs 20gms.find the minimum number of coins to get a total of 196.5gms.

9.A can do a work in 8 days, B can do a work in 7 days, C can do a work in 6 days.

A works on the first day, B works on the second day and C on the third day resly.that is they work on alternate days. When will they finish the work.(which day will they finish the work)

Ans: 7 7/168 days



10.A batsman scores 23 runs and increases his average from 15 to 16. find the runs to be made if he wants top inc the avg to 18 in the same match.

ans: 39runs.