Java Primmer Multiple Choice Questions Answers


50.What will be the output of the following Java code?
public class Person{
public void talk(){
System.out.print(“I am a Person”);
}
}
public class Student extends Person{
public void talk(){
System.out.print(“I am a Student”);
}
}
public class Test{
public static void main(String args[]){
Person p = new Student();
p.talk();
}
}
Choose the most appropriate option

ANS – I am a Student

51.Consider the following tables:
Account(AccountNumber, AccountOpeningDate, Balance). Here AccountNumber is
the primary key.
Transaction(TransactionId, AccountNumber, DateOfTransaction, Amount, TransactionType);
Here TransactionId is the primary key and AccountNumber is the foreign key
referencing AccountNumber in Account table.Which is the CORRECT query to display AccountNumber and balance of accounts on which NO transaction was done after ’31-
Dec-2015′;

ANS – SELECT AccountNumber,Balance
FROM Account
WHERE AccountNumber NOT IN
(SELECT AccountNumber FROM
Transaction
WHERE DateOfTransaction > ’31-Dec-
2015′);

52.Refer the class declarations given below:
class A{ }
class B extends A{ }
class C extends B{ }
class D extends C{ }
Identify the INCORRECT statement.
Choose the most appropriate option.

ANS – C c=new B();

53.What will be the output of the following Java code?
public class ApplicationTester {
public static void main(String[] args){
int i=0;
while(++i <= 5){
System.out.print(i + ” “);
i++;
}
System.out.println(i);
}
}

ANS – 1 3 5

54.John and Peter are working on a Java project. John is using Windows platform and Peter is using Unix platform. In order for
Peter to execute John’s Java code which file John should share with Peter?

ANS – Both Source Code (.java file) and JVM

55.What will be the output of the following Java code?
class MyClass{
public static void main(String[] args) {
char value=’2′;
switch(value){
case 1:
System.out.println(“one”);
case 2:
System.out.println(“two “);
case 3:
System.out.println(“three”);
default:
System.out.println(“default”);
}
}
}
Choose the most appropriate option.

ANS – default

56.What will be the output of the following Java code?
public class ApplicationTester {
public static void main(String[] args) {
char letterArray[]={‘a’,’A’,’h’,’h’,’m’,’m’};
for(int index=0;index<letterArray.length;index++){
if(letterArray[index] == letterArray[index+1]){
break;
}
System.out.print(index + ” “);
}
}
}
Choose the most appropriate option.

ANS – 0 1 2 3 4

57.Which of the following is/are TRUE with respect to Grey Box Testing?

(a) Testing is based on the requirements and specifications of the system internals
(b) Testing does not check the code or internal structure
(c) Testing monitors an application to check if its behavior matches with the valid input

Ans:All (a),(b) and (c)are TRUE

58.What will be the output of the following Java code?
pubilc class ApplicationTester{
public static void main(String[] args){
int array[] ={12,54,78,25,63,98,19};
int index=1;
int sum;
while(index<array.length){
sum=0;
sum=sum+array[index];
System.out.print(sum+” “);
}
}
}
Choose the most appropriate option

ANS – infintely prints 54

59.Which of the following are characteristics of Agile?

a. Deliver working software frequently, from a couple of weeks to a couple of months
b. Working software is the primary measure of progress
c. Comprehensive documentation

Ans:Both(a)and (c)

60.Refer the code given below:
public class ApplicationTester{
public static void main(String[] args){
Employee emp = new Employee();
}
}
Assume that class Employee is a valid Java class having instance variables as (employeeId of type integer and employeeName
of type String).
Choose from below a VALID option.

ANS – emp reference variable is created on
stack and employeeId and
employeeName are created on heap


61.Choose the CORRECT statement(s) with respect to aggregate functions

a) MAX and MIN functions can be applied only on numeric column
b) SUM function can be applied on alpha numeric column
c) COUNT(*) counts the number of rows in the table including NULL
d) Aggregate functions are applied on multiple columns

ANS – a and b only

62.What will be the output of the following Java code?
public class OverloadTester{
public static void main(String[] args) {
new OverloadSample().calculate(10, 20);
}
public void calculate(int num1, int num2) {
System.out.println(“Inside calculate integer”);
}
public void calculate(long num1, long num2) {
System.out.println(“Inside calculate long”);
}
public void calculate(float num1, float num2) {
System.out.println(“Inside calculate float”);
}
}

ANS – Inside calculate integer

63.What will be the output of the following Java code?
public class Tester{
public static void main(String[] args){
int array[]= new int[3];
for (int i = 0; i < array.length; i++){
System.out.print(array[i]+” “);
}
System.out.print(“–Done–“);
}
}
Choose the most appropriate option.

ANS – 0 0 0 –Done–

64.Consider the Java code given below:
public class MemoryTester{
public static void main(String[] args){
String company=new String(“Accenture”);
int employeeCount = 350000;
}
}
Which of the following statement is TRUE with respect to the above code?
Choose the most appropriate option.

ANS – company’ reference variable and
’employeeCount’ will be created in
Stack where as String object will be
created in Heap

65.What will be the output of the following Java code?
public class Tester{
public static void main(String[] args){
int num = 12;
switch (num % 2) {
default:
System.out.print(“Default “);
case 0:
System.out.print(“Even 1 “);
case 0:
System.out.print(“Even 2 “);
case 1:
System.out.print(“Odd “);
break;
}
}
}

ANS – Compilation Error as case values
cannot be duplicated

66.Consider the below tables.Actor(actorId,actorName) – actorId is primary key
Event(eventId,eventName) – eventId is primary key
Registration(registrationId,actorId,eventId) – actorId and eventId are foreign keys
Which is the CORRECT SQL statement to retrieve actorId, actorName and eventId for all actors who has registered for an
event. If any actor is not registered for any event, still the actor details should be displayed.

ANS – SELECT
a.actorId,a.actorName,r.eventId
FROM Actor a LEFT OUTER JOIN
Registration r
ON a.actorid=r.actorid;

67.A java file “FirstDemo.java” was complied on windows machine which resulted in a file “FirstDemo.class”. This file was then run
and tested on a LINUX machine. It ran successfully giving a valid output as expected.

ANS – Platform Independent

68.Refer the code given below:
public class Vehicle {
private int vehicleId;
private String vehicleName;
public Vehicle(int vehicleId, String vehicleName) {
this.vehicleId = vehicleId;
this.vehicleName = vehicleName;
}
// Assume Getters and Setters are coded
}
public class Car extends Vehicle {
private String carModel;
private double average;
//Line1
// Assume getters and Setters are already coded…
}
Choose a valid Constructor implementation to be inserted at Line1 to make the above code compile successfully
Choose the most appropriate option.

ANS – public Car(int vehicleId, String
vehicleName, String carModel,double
average) {
super(vehicleId, vehicleName);
//Java code to initialize carModel and
average
}

69.Which of the following options are the characteristics of Agile development?
(a) It delivers functional bits of the application as soon as they are ready
(b) It helps teams respond to unpredictability through incremental, iterative work

ANS – Both (a) and (b)

70.Analyse the code given below. Note that Employee.java is present in a package “pack1” and RegularEmployee.java is present
in a package “pack2”.
package pack1;
public class Employee{
public int empid;
protected float salary;
private String name;
String mailId;

}

package pack2;
import pack1.Employee;
public class RegularEmployee extends Employee{
public static void main(String[] args) {
Employee emp=new Employee();
// your code goes here
}
}
Identify the CORRECT statement which can be added in the main method in place of “your code goes here”.
Choose the most appropriate option.

ANS – System.out.println(emp.empid)

71.You are working on a critical project. Due to some project requirement you are asked to share sensitive information with others.
Which among the following actions you will perform?

a. Don’t share any sensitive information with anyone
b. Label the information as “Restricted” so that no one tries to access the information
c. Use encryption for secure sharing of information

ANS – only a


Leave a Reply

Your email address will not be published. Required fields are marked *