Since now a days the number of candidates appearing for a job has increased, companies have started keeping screening process. This screening process consist of objective questions which can be answered online. Only those candidates which score good marks in Objective questions are called for personal interviews.
This blog has multiple choice OOPS and Java questions. It is a mix of topics like Inheritance, overriding, static, constructor etc.
Read the question and try to solve it yourself. Then you can click on the Answer button to verify your answer.
Core Java Test 1 >> Core Java Test 2 >> Exception Handling Test 3
1.
2.
This blog has multiple choice OOPS and Java questions. It is a mix of topics like Inheritance, overriding, static, constructor etc.
Read the question and try to solve it yourself. Then you can click on the Answer button to verify your answer.
Core Java Test 1 >> Core Java Test 2 >> Exception Handling Test 3
1.
public class BankDetails { private int accountNumber; private boolean accountExpired; private int balance; private boolean isSaving; private boolean isCurrent; public boolean isSaving() { return isSaving; } public boolean isCurrent() { return isCurrent; } public BankDetails(int accountNumber, boolean accountExpired, int balance, boolean isSaving, boolean isCurrent) { this.accountNumber = accountNumber; this.accountExpired = accountExpired; this.balance = balance; this.isSaving = isSaving; this.isCurrent = isCurrent; } public static void main(String[] args) { BankDetails bankDetails = new BankDetails(14523, false, 500, true, false); System.out.println(getCurrentSalary(bankDetails)); } private static int getCurrentSalary(BankDetails bankDetails) { if (bankDetails.isSaving() && !bankDetails.accountExpired && (bankDetails.balance > 100)) { return bankDetails.balance; } return 0; } }
Output 1. 0 2. 500 3. Compile error
2.
package my; public class Dog extends Animal {} package my; public class Animal { public void eat() { System.out.println("Animal is eating"); } public static void main(String[] args) { i. Object obj = new Animal(); ii. Animal animal = new Dog(); iii. Object obj1 = new Dog(); iv. obj.eat(); v. animal.eat(); vi. obj1.eat(); vii. boolean areObjectEqual=obj.equals(obj1); viii. Animal a = (Animal)obj; ix. a.eat(); x. Dog dog = (Dog)obj1; xi. dog.eat(); } }
Which of the lines will not compile? 1. i, iii, iv, vi 2. iv, vi 3. ix, xi
3.
package my; public class Dog extends Animal { int i; public Dog(int i){ this.i=i; } public void eat() { System.out.println("Dog "+i+" is eating"); } } package my; public class Animal { public void eat() { System.out.println("Animal is eating"); } public static void main(String[] args) { Object object = new Object(); Object animal = new Animal(); Animal dog1 = new Dog(1); Object dog2 = new Dog(2); callEat(object); callEat(animal); callEat(dog1); callEat(dog2); } public static void callEat(Object ref){ if(ref instanceof Animal){ Animal a = (Animal)ref; a.eat(); } if(ref instanceof Dog){ Dog a = (Dog)ref; a.eat(); } } }
1. Animal is eating Dog 1 is eating Dog 2 is eating 2. Animal is eating Dog 1 is eating Dog 1 is eating Dog 2 is eating Dog 2 is eating
4.
package my; public class BankAccount { public BankAccount(int accountNumber) { numberOfAccounts++; this.accountNumber=accountNumber; } public static int numberOfAccounts; public int accountNumber; public static int getNumberOfAcoounts(){ return numberOfAccounts; } public static void main(String[] args) { System.out.println(BankAccount.numberOfAccounts); BankAccount firstAccount = new BankAccount(1449); System.out.println(BankAccount.numberOfAccounts); System.out.println(firstAccount.numberOfAccounts); BankAccount secondAccount = new BankAccount(1449); System.out.println(secondAccount.numberOfAccounts); System.out.println(firstAccount.getNumberOfAcoounts()); } }
Output: 1. Compilation error instance variable cannot access static variables 2. Constructor cannot access static variables 3. 0 1 1 2 2 4. 0 1 1 2 1
5.
package my; public class BankAccount { public BankAccount(int accountNumber) { numberOfAccounts++; this.accountNumber=accountNumber; } public static int numberOfAccounts; public int accountNumber; public static int getNumberOfAcoounts(){ return numberOfAccounts; } public static int getAccountNumber(){ return accountNumber; } public static void main(String[] args) { BankAccount bankAccount = new BankAccount(1221); System.out.println(BankAccount.getAccountNumber()); } }
Output: 1. Compilation error at line return numberOfAccounts; Can not access instance variable from static method 2. 1221
6.
package my; public class Animal { { System.out.println("In instance block"); } static{ System.out.println("In Static block"); } public Animal(){ System.out.println("In Default constructor"); } public static void main(String[] args) { Animal a = new Animal(); } }
Output: In instance block In Static block In Default constructor In Static block In instance block In Default constructor In Static block In Default constructor In instance block
For more objective question click OOPS and JAVA Objective questions part 2
your explanation is too good, Thanks for sharing this information about MSBI Online Training
ReplyDelete