Question 1 | MCQ
THIS IS QUESTION PAPER FOR THE SUBJECT"DIPLOMA LEVEL : PROGRAMMING CONCEPTS USING JAVA (COMPUTER BASED EXAM)"
ARE YOU SURE YOU HAVE TO WRITE EXAM FOR THIS SUBJECT?
CROSS CHECK YOUR HALL TICKET TO CONFIRM THE SUBJECTS TO BE WRITTEN.
(IF IT IS NOT THE CORRECT SUBJECT, PLS CHECK THE SECTION AT THE TOP FOR THE SUBJECTS REGISTERED BY YOU)
ARE YOU SURE YOU HAVE TO WRITE EXAM FOR THIS SUBJECT?
CROSS CHECK YOUR HALL TICKET TO CONFIRM THE SUBJECTS TO BE WRITTEN.
(IF IT IS NOT THE CORRECT SUBJECT, PLS CHECK THE SECTION AT THE TOP FOR THE SUBJECTS REGISTERED BY YOU)
Solution
Solution
Consider the code given below.
interface Vehicle { public abstract double getFuelEfficiency();
} class Car implements Vehicle, Cloneable { protected double fuelCapacity; protected double mileage; public Car(double fuel, double m) { fuelCapacity = fuel; mileage = m;
} public double getFuelEfficiency() { return fuelCapacity * mileage;
} public Car clone() throws CloneNotSupportedException { return (Car) super.clone();
}
} class ElectricCar extends Car { public ElectricCar(double batteryCapacity, double mileage) { super(batteryCapacity, mileage);
} public ElectricCar clone() throws CloneNotSupportedException { return (ElectricCar) super.clone();
}
} public class VehicleTest { public static void main(String[] args) { try {
ElectricCar e1 = new ElectricCar(50, 2);
ElectricCar e2 = e1.clone(); e1.fuelCapacity = 100;
System.out.print(e1.getFuelEfficiency() + e2.getFuelEfficiency());
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported");
}
}
}
What will the output be?
interface Vehicle { public abstract double getFuelEfficiency();
} class Car implements Vehicle, Cloneable { protected double fuelCapacity; protected double mileage; public Car(double fuel, double m) { fuelCapacity = fuel; mileage = m;
} public double getFuelEfficiency() { return fuelCapacity * mileage;
} public Car clone() throws CloneNotSupportedException { return (Car) super.clone();
}
} class ElectricCar extends Car { public ElectricCar(double batteryCapacity, double mileage) { super(batteryCapacity, mileage);
} public ElectricCar clone() throws CloneNotSupportedException { return (ElectricCar) super.clone();
}
} public class VehicleTest { public static void main(String[] args) { try {
ElectricCar e1 = new ElectricCar(50, 2);
ElectricCar e2 = e1.clone(); e1.fuelCapacity = 100;
System.out.print(e1.getFuelEfficiency() + e2.getFuelEfficiency());
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported");
}
}
}
What will the output be?
Solution
Solution
Solution
Solution
Consider the Java code given below.
interface Discount{ public void apply(int price);
} class ShoppingCart{ public Discount getDiscount(String type){
Discount d = null; if(type == "festival") d = new FestivalDiscount(); return d;
} private class FestivalDiscount implements Discount{ public void apply(int price){
System.out.println("Final Price: " + (price - 100));
}
}
} public class CartTest { public static void main(String[] args) {
// CODE SEGMENT // disc.apply(500);
}
}
Choose the correct option to fill in atCODE SEGMENTsuch that the output is:
Final Price: 400
interface Discount{ public void apply(int price);
} class ShoppingCart{ public Discount getDiscount(String type){
Discount d = null; if(type == "festival") d = new FestivalDiscount(); return d;
} private class FestivalDiscount implements Discount{ public void apply(int price){
System.out.println("Final Price: " + (price - 100));
}
}
} public class CartTest { public static void main(String[] args) {
// CODE SEGMENT // disc.apply(500);
}
}
Choose the correct option to fill in atCODE SEGMENTsuch that the output is:
Final Price: 400
Solution
Consider two Java files located in two different packages as shown below.
Account.java:
package com.bank; public class Account { int accountNumber = 1001; private double balance = 5000.0; protected void deposit(double amount) { balance += amount;
System.out.println("Deposited: " + amount);
} public void displayBalance() {
System.out.println("Balance: " + balance);
}
}
SavingsAccount.java:
package com.customer; import com.bank.Account; public class SavingsAccount extends Account { public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount();
System.out.println(sa.accountNumber); // LINE 1
System.out.println(sa.balance); // LINE 2 sa.deposit(1000); // LINE 3 sa.displayBalance(); // LINE 4
}
}
Choose the correct option.
Account.java:
package com.bank; public class Account { int accountNumber = 1001; private double balance = 5000.0; protected void deposit(double amount) { balance += amount;
System.out.println("Deposited: " + amount);
} public void displayBalance() {
System.out.println("Balance: " + balance);
}
}
SavingsAccount.java:
package com.customer; import com.bank.Account; public class SavingsAccount extends Account { public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount();
System.out.println(sa.accountNumber); // LINE 1
System.out.println(sa.balance); // LINE 2 sa.deposit(1000); // LINE 3 sa.displayBalance(); // LINE 4
}
}
Choose the correct option.
Solution
Consider the Java code given below.
interface Shape { void draw();
} class ShapeFactory { private String type; public void setType(String t) { type = t;
} public String getType() { return type;
} public Shape createShape() { switch (getType()) { case "Circle":
return new Circle(); case "Square":
return new Square();
case "Triangle":
return new Triangle(); default:
return null;
}
} private class Circle implements Shape { public void draw() {
System.out.println("Drawing Circle");
}
} private class Square implements Shape { public void draw() {
System.out.println("Drawing Square");
}
} private class Triangle implements Shape { public void draw() {
System.out.println("Drawing Triangle");
}
}
} public class ShapeDemo { public static void main(String[] args) {
ShapeFactory sf = new ShapeFactory(); sf.setType("Square");
// --------- LINE 1 ---------
}
}
Identify the appropriate option(s) to fill in place ofLINE 1such that the output is: Drawing Square
interface Shape { void draw();
} class ShapeFactory { private String type; public void setType(String t) { type = t;
} public String getType() { return type;
} public Shape createShape() { switch (getType()) { case "Circle":
return new Circle(); case "Square":
return new Square();
case "Triangle":
return new Triangle(); default:
return null;
}
} private class Circle implements Shape { public void draw() {
System.out.println("Drawing Circle");
}
} private class Square implements Shape { public void draw() {
System.out.println("Drawing Square");
}
} private class Triangle implements Shape { public void draw() {
System.out.println("Drawing Triangle");
}
}
} public class ShapeDemo { public static void main(String[] args) {
ShapeFactory sf = new ShapeFactory(); sf.setType("Square");
// --------- LINE 1 ---------
}
}
Identify the appropriate option(s) to fill in place ofLINE 1such that the output is: Drawing Square
Solution
Solution
Consider the Java code given below that prints the average rating of a set ofRateableobjects. From among the options, identify the appropriate function header for the functionprintAverageRatingthat takes as input an array ofRateableobjects and prints the average rating.
import java.util.*; interface Rateable { public abstract double getRating();
} class Product implements Rateable { private double rating;
// Constructor
// method getRating() that returns rating of Product
} public class Test {
// LINE 1: FUNCTION HEADER
{
// invokes method getRating()
// to compute and print the average rating
} public static void main(String[] args) {
Rateable[] products = {new Product(4.5), new Product(3.8), new Product(5.0)}; printAverageRating(products);
}
}
Choose the correct option(s).
import java.util.*; interface Rateable { public abstract double getRating();
} class Product implements Rateable { private double rating;
// Constructor
// method getRating() that returns rating of Product
} public class Test {
// LINE 1: FUNCTION HEADER
{
// invokes method getRating()
// to compute and print the average rating
} public static void main(String[] args) {
Rateable[] products = {new Product(4.5), new Product(3.8), new Product(5.0)}; printAverageRating(products);
}
}
Choose the correct option(s).
Solution
Solution
Consider the Java code given below.
class Student { private int marks; public Student(int marks){ assert marks >= 0 && marks <= 100 : "bad marks"; // assert-1 this.marks = marks;
}
}
class LoanApplication { private double loanAmount; public LoanApplication(double loanAmount){ assert loanAmount > 0 : "bad loan"; // assert-2 this.loanAmount = loanAmount;
}
}
class Product { private int quantity; public Product(int quantity){ assert quantity >= 0 : "bad qty"; // assert-3 this.quantity = quantity;
}
}
public class FClass { public static void main(String[] args){
LoanApplication l = new LoanApplication(-50000);
Product p = new Product(10);
Student s = new Student(105);
}
}
Identify theassertstatement that throws theAssertionErrorwhen the class is executed as: java -ea:Student -da:LoanApplication FClass
class Student { private int marks; public Student(int marks){ assert marks >= 0 && marks <= 100 : "bad marks"; // assert-1 this.marks = marks;
}
}
class LoanApplication { private double loanAmount; public LoanApplication(double loanAmount){ assert loanAmount > 0 : "bad loan"; // assert-2 this.loanAmount = loanAmount;
}
}
class Product { private int quantity; public Product(int quantity){ assert quantity >= 0 : "bad qty"; // assert-3 this.quantity = quantity;
}
}
public class FClass { public static void main(String[] args){
LoanApplication l = new LoanApplication(-50000);
Product p = new Product(10);
Student s = new Student(105);
}
}
Identify theassertstatement that throws theAssertionErrorwhen the class is executed as: java -ea:Student -da:LoanApplication FClass
Solution
Solution
Consider the Java code given below.
interface Chargeable { void showChargeStatus();
} class Laptop implements Chargeable { public void showChargeStatus() {
System.out.println("Laptop is charging");
}
} class Mobile implements Chargeable { public void showChargeStatus() {
System.out.println("Mobile is charging");
}
} class DeviceList { private Object[] devices = {new Laptop(), new Mobile()}; public void testDevices() { for (int i = 0; i < devices.length; i++) {
//LINE-1
}
}
} public class TestDevices { public static void main(String[] args) {
DeviceList dL = new DeviceList(); dL.testDevices();
}
}
Identify the appropriate option to fill in place ofLINE-1such that the output is: Laptop is charging
Mobile is charging
interface Chargeable { void showChargeStatus();
} class Laptop implements Chargeable { public void showChargeStatus() {
System.out.println("Laptop is charging");
}
} class Mobile implements Chargeable { public void showChargeStatus() {
System.out.println("Mobile is charging");
}
} class DeviceList { private Object[] devices = {new Laptop(), new Mobile()}; public void testDevices() { for (int i = 0; i < devices.length; i++) {
//LINE-1
}
}
} public class TestDevices { public static void main(String[] args) {
DeviceList dL = new DeviceList(); dL.testDevices();
}
}
Identify the appropriate option to fill in place ofLINE-1such that the output is: Laptop is charging
Mobile is charging
Solution
Consider the Java code given below.
class NegativeBalanceException extends Exception { public NegativeBalanceException() { super("Transaction failed: negative balance");
}
}
class BankAccount { private double balance; public BankAccount(double b) { balance = b;
}
public void withdraw(double amount) throws NegativeBalanceException { if(balance - amount < 0) throw new NegativeBalanceException(); balance -= amount;
System.out.println("Withdrawal successful: " + balance);
}
}
public class TestBank { public static void main(String[] args) { try {
BankAccount acc1 = new BankAccount(500);
BankAccount acc2 = new BankAccount(200); acc1.withdraw(600); acc2.withdraw(150);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Choose the correct option.
class NegativeBalanceException extends Exception { public NegativeBalanceException() { super("Transaction failed: negative balance");
}
}
class BankAccount { private double balance; public BankAccount(double b) { balance = b;
}
public void withdraw(double amount) throws NegativeBalanceException { if(balance - amount < 0) throw new NegativeBalanceException(); balance -= amount;
System.out.println("Withdrawal successful: " + balance);
}
}
public class TestBank { public static void main(String[] args) { try {
BankAccount acc1 = new BankAccount(500);
BankAccount acc2 = new BankAccount(200); acc1.withdraw(600); acc2.withdraw(150);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Choose the correct option.
Solution
Shape
Thickness [ / ]
3
All colors