Showing posts with label AsCEnD Advanced Java Programming. Show all posts
Showing posts with label AsCEnD Advanced Java Programming. Show all posts

Wednesday, 27 May 2020

AsCEnD Advanced Java Programming

Java Advanced & Java Part 1 Advanced Assessment

Q1. Which of the following statements are correct?

Answer: Strings in java are immutable.
              Every string is an object of class String.
              StringBuffer class is used to store string in a buffer for later use.
              Java defines a peer class of String, called StringBuffer, which allows string to be altered.

Q2. Which of the following is not a wrapper class in java?

Answer: Char

Q3. Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

Answer: java util ArrayList

Q4. Which of these are runtime exceptions in JAVA ?

Answer: NullPointerException
              ArrayIndexOutOfBoundsException

Q5. Why are generics used?

Answer: Generics add stability to your code by making more of your bugs detectable at compile time.

Q6. To explicitly throw an exception, the keyword _______ is used

Answer: throw

Q7. ArrayList is synchronized

Answer: false

Q8. Which of the following statement is incorrect?

Answer: FileNotFoundException is a runtime exception

Q9. Is it possible to do both of the following. Override protected method to public Override protected method to private

Answer: Only option 1 is possible. You can override protected to public, but not protected to private.

Q10. Which of these interfaces only store unique elements?

Answer: Set

Q11. Select one or more true statements

Answer: 
List implementation dynamically grow in size, allow duplicates and are implicitly not sorted
A set does not store duplicate values
HashMap and Hashtable both store key value pairs, Hashtable we would use in case of synchronization requirements.
All Set implementations are sorted

Q12. Which of these is a correct statement?

Answer: 
Abstract class can be inherited.
Abstract class defines only the structure of the class not its implementation.
Every class containing abstract method must be declared abstract.

Q13. A method can specify its ability to throw an exception by using the ______ keyboard.

Answer: Throws

Q14. Is it possible to define a class so that it cannot be subclassed?

Answer: Yes, by making the class final

Q15. Which of the following statement is incorrect?

Answer: final methods can be overridden

Q16. public class Test{
public static void main(String args[]){
int k=0;
try{
 int i=5/k;
}
catch (ArithmeticException e){
System.out println("1");
}
catch (RuntimeException e){
System out println("2");
return
catch (Exception e){
System.out.println("3");
}
finally{
System.out.println("4");
}
System.out println("5");
}
}
What would be the output of above program?

Answer: 145

Q17. Assume we have the following classes

class ParentClass
{
int id;
class ChildClass extends ParentClass
{
String name;
}
public class MainClass
{
{
//Withings go here
}
}
Which statements are valid to use in our main method?

Answer: ChildClass x = new ChildClass();
       ParentClass x = new ParentClass();
       ParentClass x = new ChildClass();

Q18. Would this program compile?

interface A{
public void method 1();
}
interface B{ 
public void method 1();
}
public class Test implements A, B{
public void method 1(){
}
}

Answer: Yes, It will compile successfully.

Q19. What will be the output?
try{
int x=;
int y = 5/x;
}
catch (Exception e){
System.out println("Exception");
}
Catch (ArithmeticException ae){
System.out.printin(" Arithmetic Exception
catch (ArithmeticException ae){
System.out.println(" Arithmetic Exception");
}
System.out println("finished");

Answer: Compilation fails

Q20. What will be the output?
import java.util.*;
class Output{
public static void main(String args[]){
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("A");
System.out.println(obj.size());
}
}

Answer: 2

Q21. Map<String, String> obj = new HashMap<String, String>();
obi.put("City","Goa");
obj put("City","Shimla");
obj put("City","Pune");
obj put("CityName", "Shimla");

Select one or more true statements for above code

Answer: Values in a Map can be duplicated
       Following values will be stored in HashMap {CityName=Shimla, City= Pune}

Q22. What will be the output?

import java.util.HashMap;
import java.util.Map;
public class MapTest{
public static void main(String args[]) {
Map m = new HashMap();
m.put(null, "Test");
m.put(null, "Fest");
System.out.println(m);
}
}

Answer: {null=Fest}

Q23. What is the output of the below code:

public static void main(String args[])
{
ArrayList<String> myList = new ArrayList<String>();
myList.add("item I");
myList.add("Another Item");
myList.add("Last Item");
for(String s : mylist)
{
System.out.print(s.charAt(0));
}
}

Answer: IAL

Q24. What will be the output?

import.java.util.*;
class Output {
public static void main(String args])
{
ArrayList obj = new ArrayList();
obj add("A");
obj remove("A");
System.out.println(obj.size());
}
}

Answer: 0

Q25. What is the output of the below code:

public static void main(String args[]){
ArrayList myList = new ArrayList();
myList.add("item I");
myList.add("Another Item");
myList.add("Last Item");
for(String s : mylist)
{
System.out.print(s.charAt(0));
}
}

Answer: IAL

Q26. Consider the following class.
class Test
{
int x;
public static void main(String[] args)
{
this.x=5; // Currently shows an error
}
}

Answer: main cannot access this.x

Q27. What will be the output?

class exception_handling {
public static void main(String args[]){
try {
int a = args.length;
int b = 10 / a;
System.out.print(a);
try {
if (a == 1);
a = a/a-a;
if (a == 2){
int c = {1};
c[8]= 9;
}
}
catch (ArrayindexOutOfBounds Exception e) {
System.out.println("Type");
catch ArithmeticException e){
System.out.printin("TypeB");
}
}
}

Answer: Compilation error

Q28. public class Test{
public static void main(String args[]){
try{
int answer 20/0;
}
catch(ArithmeticException e) {
//some code
}
catch (Exception e) {
//some code
catch (NullPointerException e){
//some code
}
}

Answer: 
We will get a compilation error for an unreachable catch block because NullPointerException is already handled by the catch block for exception
A Try block can have multiple catch blocks but the most generalized catch block should be the last one

Q29. public class Test{
enum eColors{
Black (5),
BLUE (10),
GREEN (15),
int color Code=0;
eColors(int colorCode){
this.colorCode=colorCode;
}
}
public static void main(String args[]){
System.out.println(color.values()[1].colorCode);
}
}
What will be the output?

Answer: 10

Q30. What will be the output?
public class RTExcept
{
public static void throwit ()
{
System.out.print("throw it");
throw new RuntimeException();
}
public static void main(String (] args)
{
try
{
System.out.println("hello);
}
catch (Exception re)
{
System.out.println("caught");
}
finally
{
System.out.print("finally");
}
System.out.println("after");
}
}

Answer: hello throwit caught finally after