Может кому пригодится.
MATH Test
(Time Allowed: 1hr)
Instructions: Please answer the following questions using the information given. No assumptions are necessary.
B=1p+1/2*B => B=2p1. A brick weighs a pound plus 1/2 a brick. How much does a brick weigh?
Answer: 2 pounds;
Let’s compare volumes of the cylinder and a cube:2. Could a stack of pennies as high as the C.N. Tower fit into a 2x2x2 foot box? (A penny has a radius of 3/8 inch, and the C.N. Tower is 1900 feet high)
Vcyl = 1900 *3.14*9/64=3835.29;
Vcube = 2*2*2=8;
Conclusion: does not matter how we fill the box (heap or many stacks) it still will not fit.
1. I+(F-I) = 5*s;3. When I am as old as my father is now, I will be five times as old as my son is now. By then my son will be eight years older than I am now. The combined ages of my father and me are 100 years. How old is my son?
2.S+(F-I) = I + 8; => 1. F= 5*S; 2. S= 308 -3*F; => S=22;
3. I = 100 – F;
Answer: Son is 22 years old;
1. Fmax()=Sal – (Sal/100)*Dpw4. Citizens of Canada pay as much income tax (percentage-wise) as they make dollars per week. What is the optimal salary in Canada?
2. Dpw * 52.18 = Sal.
Where Sal – salary, Dpw – dollars per week, 52.18 – weeks per year.
Fmax()= -Sal**2/5218 +Sal
For max first derivative should be 0: -Sal/2609 +1 =0 => Sal= 2609
Answer: best salary is $2609/year.
1. 9*Bh1=10*Bh25. A contractor estimated that one of his two bricklayers would take 9 hours to build a certain wall and the other 10 hours. However, he knew from experience that when they worked together, 10 fewer bricks got laid per hour. Since he was in a hurry, he put both men on the job and found it took exactly 5 hours to build the wall. How many bricks did it contain?
2. Bh1-Bh2=10 => Bh1=100; B=5*190=950;
3. B = 5(Bh1 + Bh2)
Answer: the wall contains 950 bricks;
4 odd numbers in first 7 numbers;6. The numbers one through seven are mixed up and then drawn from a hat without replacement. What is the probability that all of the odd numbers will be drawn first?
P = P1*P2*P3*P4 = 4/7 * 3/6 * 2/5 * ¼ = 1/35
Where P1 = probability to draw odd number out of 7 numbers
P2 – probability to draw odd number out of 6 numbers
P3 – probability to draw odd number out of 5 numbers
P4 – probability to draw odd number out of 4 numbers
Answer: P= 1/35
Do not get the question; If a fly already did all the rounds why are you asking “What are a fly’s chances”?7. A spider needs to eat three flies a day. Until he fills his quota, he has an even chance of catching any fly that attempts to pass his web. What are a fly's chances of survival, given that five flies have flown by today and no more will?
Should it be “What were a fly’s chances”?
Ps=2*Pj;8. Smith's chance of hitting his target on a given shot is twice that of his fellow prison guard, Jones. One dark night they each had time to fire one shot at an escaping prisoner. Given that the prisoner had an even chance of avoiding injury, what kind of a marksman was Jones?
Phit = Ps+Pj = 1/2;
Pj = 1/2– 2*Pj;
Pj = 1/6;
Answer: Probability that Jones will injures the prisoner is 1/6.
Programming
1. What are the advantages of using composition as opposed to inheritance?
Advantages:
Less work for VM to maintain inheritance.
Simple approach off of two.
Doesn’t have to be an object to use composition.
No attribute conflict.
Isolation – composee object doesn’t know about composer(
Much looser coupling)
• Someone tries to set the capacity of a PackingBox to a negative value.2. Which of the following circumstances should throw Exceptions? Explain your reasoning.
Should be an exception, but not terminal for the application. Depending on approach can be validated right away or upon form submission, on client or server side (if web form).
• A syntax error is found in a configuration file that an object uses to set its initial state.
Depends on type of configuration value. For values which can be defaulted warning message in log file should be enough. Values failed validation should throw exception.
• A method that searches for a programmer-specified word in a string array cannot find any occurrence of the word.
Depends on what is returned from the method. If count of matched rows in string array – no exception, just return 0(zero). If returning subset of matching rows – generate an exception if no matches found.
In a non-insatiable class with static members only.3. What are some situations where you would make constructors private?
When you want to hide the copy ctor.
Java
Code: Select all
1. Upgrade the Person class below to implement the Comparable interface so that it sorts first by last name then by first name.
Code: Select all
class Person implements Comparable{
String m_firstName;
String m_lastName;
public String get_FirstName() { return m_firstName; }
public String get_LastName() { return m_lastName; }
public int compareTo(Object PersonToCompareTo) Throws Exception{
int lnWeight=10;
int fnWeight=1;
if ((Person)PersonToCompareTo.get_LastNAme().ToString().equalsIgnoreCase()(
this.get_LastName())){
if ((Person)PersonToCompareTo.get_FirstNAme().ToString().equalsIgnoreCase()(
this.get_FirstName())){
return 0;
}
else { return fnWeight;}
} else {
if ((Person)PersonToCompareTo.get_FirstNAme().ToString().equalsIgnoreCase()(
this.get_FirstName())){
return lnWeight;
}
else { return lnWeight +fnWeight;}
}
}
}
2. Change the function divide() to throw an exception if the den is zero and change the function fun() to catch the exception.
Code: Select all
Public class Zero_Exception extends exception(){
Public Strin toString(){
return ”Zero exception!”;
}
}
class SomeClass {
void divide( int num, int den) {
double nd_dev;
Exception e;
nd_dev= num / den;
if(nd_dev==0) {
throw new Zero_Exception();
}
}
void fun() {
try(
divide( 4, 2);
}
catch(Zero_Exception e){
System.out.println( e);
}
}
}
3. Implement the addToBeginningOfList method below.
Code: Select all
class Node {
public int value;
public Node next;
}
class LinkedList {
private Node first;
public void addToBeginningOfList( Node node ) {
node.next=this.first;
this.first=node;
}
}
Database
Consider the following model to support the display of a restaurant menu.
• A menu item has a name, a description, and a price (in US dollars). E.g. “Grilled Salmon”, “Freshly caught, herb encrusted, and lightly grilled”, “$20.00”.
• A menu consists of a set of item categories: “appetizers”, “soups”, “salads”, etc.
• Each menu item belongs to exactly one category.
• The ordering of the categories on the menu and the ordering of the items within a category is specified.
• Some items are available only on specified days of the week. E.g. “Grilled Salmon” only on Monday, Tuesday, and Friday.
• A menu will include a “multi-course meal” section that lists multiple sets of menu items at a special price. Typically, a set would consist of an appetizer, soup, or salad plus an entrée and a dessert but any combination is possible. Each set of items is given a name and is available for a single, specified price. A multi-course item is only available on a day when all of its items are available. E.g. “Potato Soup”, “Grilled Salmon”, and “Apple Pie” for $25.00.
Define database tables for this model. Provide a DDL description if possible or a pseudo-DDL description otherwise. Include constraints such as primary key, foreign key, unique, and check.
Code: Select all
Table Menu_Item (
Menu_Itam_Id (PRIMARY KEY) not null,
Menu_Item_Name,
Menu_Item_Description,
Menu_Item_Price,
)
Table Category(
Category_Id (PRIMARY KEY) not null,
Category_Name
)
Table Menu (
Order_Of_Category not null,
Order_Of_Item not null,
Menu_Item_Id (FOREIG_KEY referencing Menu_Item.Menu_Item_Id),
Category_id (FOREIGN LEY referencing Category.Category_id),
Available_On
PRIMARY_KEY(Menu_Item_Id, Category_id)
)
Table Multi_Cource_Meal(
MCMeal_Id (PRIMARY_KEY) not null,
MCMeal_Name,
Qty_Of_Menu_Items,
MCMeal _Price
)
Table Multi_Cource_Menu(
MCMeal_Id (FOREIGN_KEY referencing Multi_Cource_Meal. MCMeal_Id),
Menu_Item_Id (FOREIG_KEY referencing Menu_Item.Menu_Item_Id)
)
Write a SQL statement to fetch a list of the names of “appetizers” for Monday ordered by their prescribed order.
Code: Select all
SELECT mi.Menu_Item_Name
FROM Menu_Item mi, Category c, Menu m
WHERE m.Category_id = c.Category_id
AND m.Menu_Item_Id=mi.Menu_Item_Id
AND c.Category_Name=’appetizers’
AND BIT_AND(m.Available_On, 1 /*1- for Monday*/) !=0
ORDER BY m.Order_Of_Item desc
In SQL, which days do not offer a “soup”?
Code: Select all
SELECT WEEKDAY(day_num) week_day_name
FROM Category c, Menu m,
(SELECT rownum day_num
FROM dual
CONNECT BY LEVEL <= 7) week_days,
WHERE m.Category_id = c.Category_id
AND c.Category_Name=’soup’
AND BIT_AND(m.Available_On, day_num) =0
In SQL, how many multi-course meals are available on Thursday?
Code: Select all
SELECT COUNT(*) Qty_Available FROM (
SELECT mcml.MCMeal_Name
FROM Multi_Cource_Meal mcml, Multi_Cource_Menu mcmn, Menu m
WHERE m. Menu_Item_Id=mcmn.Menu_Item_Id
AND mcmn.MCMeal_Id= mcml. MCMeal_Id
GROUP BY mcml.MCMeal_Name
HAVING sum(DECODE(BIT_AND(m.Available_On, 4 /*4- for Thursday*/), 0, 1, 0)) = mcml. Qty_Of_Menu_Items
)
C++
Answer:1. What is the difference between malloc and new? Why is one preferable to the other?
New is a (C++) operator, but malloc is a function from C library.
New will throw an exception (by default) if there’s no memory, but maloc will return NULL.
Malloc will not call a ctor.
With malloc you have to explicitly specify the object size.
Typecast required if calling malloc
Use malloc if you need better control of raw heap memory allocation.
Use malloc if you want to dynamically resize the allocated memory (with realloc)
Use new if you need to call a ctor/dtor, or to use overloading.
Use malloc if you need fine grained control over memory allocation.
2. memset is sometimes used to initialize data in a constructor like the example below. What is the benefit of initializing this way? Does it work in this example? Does it work in general ? Is it a good idea in general?
Code: Select all
class A {
public:
A();
private:
int a;
float f;
char str[35];
long *lp;
};
A::A()
{
memset(this, 0, sizeof(*this));
}
It will not work in general (not for virtual class).
It may work if all the variable formats are specified in class definition.
Will not work on non-POD classes (the ones only C++ can handle)
Math
1) When customers trade we charge them commission. An order may get broken up into multiple trades. The commission is based on the number of shares executed for their order. (A negative quantity indicates a “sell”.) Calculate the commission for the trades below:
Commission Schedule
<= 500 shares .013/share
> 500 shares .008/share
Order Number Symbol Quantity Commission
1 IBM 450 5.85
1 IBM 150 1.95
3 INTC -500 6.5
4 KO 1,000 8
4 KO -950 7.6
2) Assume a customer can borrow up to 50% of his stock portfolio value to buy additional stock. For example if a customer has $10,000 worth of IBM and 0 cash they can buy an additional $5,000 of stock. How much more stock can the customer below buy before he runs out of margin room?
Current IBM Price: 104.00
Current ORCL Price: 20.30
Current Cash Balance: -100,000
Current IBM Position: 5,000 shares
Current ORCL Position: 10,000 shares
Answer:
$100.000 is equivalent of ~962 IBM shares or ~4927 ORCL shares.
Considering this, customer can buy 1538 IBM shares and 5,000 ORCL shares, or 2,500 IBM shares and 73 ORCL shares.