Java Fundamentals

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Java Fundamentals

Name:

E-Mail:

Mobile:

---- **** ---- **** ----

Q. What is the value of Y after the following expressions are


evaluated?

int X = 10 ;
int Y = X << 2 ;

Answer:

Q. What is the value of Y after the following expressions are


evaluated?

int X = 16 ;
int Y = X ^ 32 ;

Answer:
Q. What is the value of Z after the following expressions are
evaluated?

int X = 32 ;
long Y = 16 ;
int Z=X*Y;

Answer: Choose one of the following:

a. 48
b. 512
c. This will not compile

Q. Consider the following list. What is the expression that will give
the number of elements in this list?

List<Integer> list = new ArrayList<>() ;


list.add(1) ;
list.add(2) ;
list.add(3) ;
list.add(4) ;
list.add(5) ;

Answer:
Q. Is the following statement a valid method declaration:

protected static void TestMethod(int a, long y)

Answer: Choose one of the following:

a. Yes b. No

Q. Is the following statement a valid method declaration:

protected static void TestMethod(int a, long y)

Answer: Choose one of the following:

a. Yes b. No
Q. Consider the following class and choose the correct answer from
the options given below.
public class TestClass
{
private int number ;

private TestClass()
{
number = 10 ;
}

public TestClass getInstance()


{
return (new TestClass()) ;
}

public int getNumber()


{
return (number) ;
}
}

Answer: Choose one of the following:

a. This will not compile.


b. You can create an instance of TestClass from classes in other
packages.
c. You cannot create an instance of TestClass from classes in
other packages.
d. You can create an instance of TestClass through a call to
getInstance().
d. You cannot create an instance of TestClass.
Q. Declare a Map (that uses the Key as String and Value as String)
and add two elements (key~value pairs) to it.

Answer:

Q. Declare a Set (that can hold a set of values of type String) and
add two elements to it.

Answer:
Q. Declare an array that can hold a set of numbers and initialise it
with numbers from 1 to 10.

Answer:

Q. Write a minimal class (named Worker) that can be a starting


point for a thread and will print "Hello" when executed through
thread.

Answer:
Q. Write the statement that will put a thread to sleep for 2 seconds.

Answer:

Q. Write an interface (name Compute) that has four methods


named add, subtract, multiply and divide. The methods will take
two integers each and will return the result of type integer.

Answer:
Q. Consider the following class:

public class TestClass


{
private static int sequence = 0 ;

public void initialise()


{
sequence = 0 ;
}

public int getNextSequence()


{
return (sequence++) ;
}
}

Answer:

a. Is this class Thread Safe?

B. If not, how can you make it thread safe?


Q. Write a method that can take the file name as a parameter and
display the size of file on the console.

Answer:

Q. Write a method that can take the folder name as a parameter


and display the file names (present in the folder) on the console.

Answer:
Q. Write a method that will take the text file name as argument and
return the number of lines in it.

You might also like