Sunday, 2 August 2015

Goldman Sachs Coding Questions

Q1 Given an array, Write a program to print 3 index of the array such that sum of the contents at those index is 0?
Note: Print the index in ascending order and consider that there is only one unique solution in the array.

Input: 4 -1 0 1 5
where 4 is the length of the array and rest are the contents of the array

Output: 0 1 2
Because -1 + 0 + 1 = 0


Q2 Write a Java program to  evaluate a mathematical expression based on the rules of BODMAS?
B - Bracket ()
O - Order
D - Division
M - Multiplication
A - Addition
S - Subtraction

Assume that input may only contain + - * / ( ) and 0-9.

Input Format: 5 1 + 4 * 3
where 5 is the length and rest are the contents of the array

Output: A single integer which is the result of the expression
Above is evaluated as (1+(4*3)) = (1+12) = 13


Q3 Find the output of the following program

String s1 = new StringBuilder("Display").toString();
String s2 = "Display";
System.out.println(s1 == s2);



Q4 Find the output of below program

class Test {
    final String display = "Display";
    public static void main(String args[]) {
         System.out.println(display);
    }
}


Q5 Find the output of the below program
class Keyboard {
}
class Computer implements Serializable {
    Keyboard key = new Keyboard();

    public static void main( String args[]) {
        Computer c = new Computer();
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("test.txt"));
        os.writeObject(c);
    }
}

Q6 Find the output of the below program
class Test {
    public static void swap(String s1, String s2) {
        String temp = s2;
        s2 = s1;
        s1 = temp;
    }
    public static void main(String args[]) {
        String s1 = "World", s2 = "Hello";
        Test.swap(s1,s2);
        System.out.println(s1 + " " + s2);
    }
}

Q7 Find the output of the below program
class Test {
    public static void main(String args[]) {
        InputStream is = new FileInputStream("test.java");
        System.out.println(is.available());
    }
}

Q8 Find the output of the below program
class Test {
    public static void main(String args[]) {
        HashMap m = new HashMap();
        m.put("G", new Integer(1));
        m.put("F", new Integer(2));
        System.out.println(m);
    }
}
Q9 Consider below interface and select the correct option-
public interface Key {
    public abstract void a() throws Exception;
}
a. The class that implement the interface should provide a valid implementation of method a()
b. The class that implement the interface should define a and throws a subclass of Exception
c.
d. None of the above

Q10 Which will legally declare, construct, and initialize an array?
A. int [] myList = {"1", "2", "3"};
B. int [] myList = (5, 8, 2);
C. int myList [] [] = {4,9,7,0};
D. int myList [] = {4, 3, 7};

Q11 Which of the following does not implement the collection interface?

A. Set
B. List
C. Map
D. None of the above

Q12 Which of the following method will convert an object into a List?

A. singletonList()
B. convertList()
C. copyList()
D. None of the above


Q13 Select the ones that are thread-safe in Java.

A. HashTable
B. StringBuffer
C. HashMap
D. StringBuilder
E. All the above

Q14 Which of the following allows insert and delete at random positions?

A. ArrayList
B. Vector
C. LinkedList
D. Stack


Comment below if you find any correction. Thanks

No comments :

Post a Comment