My Report

Java Programming Practice Test 9


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What will be the output of the following Java program?

    import java.util.*;
    public class genericstack <E> 
    {
        Stack <E> stk = new Stack <E>();
	public void push(E obj)
        {
            stk.push(obj);
	}
	public E pop()
        {
            E obj = stk.pop();
	    return obj;
	}
    }
    class Output
    {
        public static void main(String args[])
        {
            genericstack <String> gs = new genericstack<String>();
            gs.push("Hello");
            System.out.print(gs.pop() + " ");
            genericstack <Integer> gs = new genericstack<Integer>();
            gs.push(36);
            System.out.println(gs.pop());
        }
    }

2. What will be the output of the following Java code?

    import java.util.*;
    public class genericstack <E>
    {
        Stack <E> stk = new Stack <E>();
	public void push(E obj)
        {
            stk.push(obj);
	}
	public E pop()
        {
            E obj = stk.pop();
	    return obj;
	}
    }
    class Output
    {
        public static void main(String args[])
        {
            genericstack <Integer> gs = new genericstack<Integer>();
            gs.push(36);
            System.out.println(gs.pop());
        }
    }

3. Which of these data type cannot be type parameterized?

4. Which feature of java 8 enables us to create a work stealing thread pool using all available processors at its target?

5. What will be the output of the following Java program?

    public class BoxDemo
    {
        public static <U> void addBox(U u, 
        java.util.List<Box<U>> boxes)
        {
           Box<U> box = new Box<>();
           box.set(u);
           boxes.add(box);
        }
        public static <U> void outputBoxes(java.util.List<Box<U>> boxes) 
        {
            int counter = 0;
            for (Box<U> box: boxes) 
            {
                U boxContents = box.get();
                System.out.println("[" + boxContents.toString() + "]");
                counter++;
            }
        }
        public static void main(String[] args)
        {
            java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
            BoxDemo.<Integer>addBox(Integer.valueOf(0), listOfIntegerBoxes);
            BoxDemo.outputBoxes(listOfIntegerBoxes);
        }
    }

6. What type of methods an interface contain by default?

7. What will be the output of the following Java code?

public class Shape 
{
	public int area()
        {
		return 1;
	}
}
public class Square extends Shape 
{
	public int area()
        {
		return 2;
	}
}
public class Rectangle extends Shape 
{
	public int area()
        {
		return 3;
	}
}
class Main() 
{
   public static void main(String[] args)
   {
	Shape shape = new Shape();
	Square square = new Square();
    	Rectangle rect = new Rectangle();
	rect = (Rectangle)shape;
	System.out.println(square.area());
   }
}

8. Which of the following keywords are used for lower bounding a wild card?

9. What will be the output of the following Java code?

public class Shape 
{
	public int area()
        {
		return 1;
	}
}
public class Square extends Shape 
{
	public int area()
        {
		return 2;
	}
}
public class Rectangle extends Shape 
{
	public int area()
        {
		return 3;
	}
}
class Main() 
{
       public static void main(String[] args)
       {
	 Shape shape = new Shape();
	 Square square = new Square();
   	 Rectangle rect = new Rectangle();
	 rect = (Rectangle)square;
	 System.out.println(square.area());
	}
}

10. What will be the output of the following Java program?

    import java.util.*;
    public class genericstack <E>
    {
        Stack <E> stk = new Stack <E>();
	public void push(E obj) 
        {
            stk.push(obj);
	}
	public E pop() 
        {
            E obj = stk.pop();
	    return obj;
	}
    }
    class Output
    {
        public static void main(String args[])
        {
            genericstack <String> gs = new genericstack<String>();
            gs.push("Hello");
            System.out.println(gs.pop());
        }
    }

 

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.