
- 1) What will be the output of the following code?
interface Test {
void print();
}
public class MyClass implements Test {
void print() {
System.out.println("Hello");
}
public static void main(String[] args) {
MyClass m = new MyClass();
m.print();
}
}
a. Hello
b. Compilation error
c. Runtime error
d. None of the above -
2) Predict the output of the following code:
class Solution {
static int x = 3;
void increment() {
x++;
}
void show() {
System.out.println("x = " + x);
}
}public class Test_Questions {
public static void main(String[] args) {Solution a = new Solution();
Solution b = new Solution();
a.increment();
b.increment();
a.show();
b.show();
}
}
a. x = 4
x = 4
b. x = 3
x = 3
c. x = 5
x = 5
d. Compilation error - 3) What will be the output of the following program?
class Base {
public Base() {
System.out.print("8");
}
}
class Sub extends Base {
public Sub() {
System.out.print("5");
super();
}
public static void main(String[] args) {
Base b = new Sub();
}
}
a. 85
b. 58
c. Compilation error
d. Runtime error -
4) Predict the output of the following code:
abstract class Top {
private String name;
Top(String name){
this.name = name;
}
public String getName() {
return name;
}
}public class Test extends Top{
Test() {
super("Amit");
}
public static void main(String[] args) {
Top t = new Test();
System.out.println(t.getName());
}a. Compilation error will occur because abstract class cannot have constructor
b. Compilation error will occur because abstract class must have an abstract method
c. Compilation error will occur while invoking super class constructor
d. Code will compile successfully and will print Amit -
5). Predict the output of the following code:
class Two extends One {
final public int getOutput(int x, int y) { return x + y; }
}
class One extends Test_Questions {
public int getOutput(int x, int y) { return x * y; }
}public class Test_Questions {
public static void main(String[] args) {One o = new Two();
System.out.println("x = " + o.getOutput(0, 1));
}
}
a. x = 1
b. x = 0
c. Compilation error
d. Runtime error - Short answer type question 10 marks
• Explain the different access modifiers in detail with examples.
• Explain the difference between abstract class and interface with example. - Long answer Type question 10 marks
A) Write a program to print the sum of two complex numbers by creating a class named as
‘ComplexNumbers’ containing two data members, real and imaginary and two functions. First
function named as ‘plus’ adds the given two complex numbers and updates the first complex
number with the result and the second function named as 'print' prints the given sum in the format
‘a + ib’ where a is the real part and b is the imaginary part. Take the real and imaginary parts of
both the complex numbers as input from scanner and consider them to be positive integers.
Input Format :
Line 1 : Two integers- real and imaginary part of 1st complex number
Line 2 : Two integers- real and imaginary part of 2
nd complex number
Sample Input :
4 5
6 7
Sample Output :
10 + i12
B) Create an interface named ‘Figure’ with an abstract void method ‘shape()’. Now create two classes
‘Rectangle’ and ‘Triangle’ implementing the ‘Figure’ interface and having the method ‘shape()’
which will print “This is rectangular shape” and “This is triangular shape” respectively. Also create a
subclass ‘Square’ of ‘Rectangle’ having the method ‘shape()’ to print “Square is a rectangle with
equal sides”. Now call the method of ‘Rectangle’ and ‘Square’ class by the object of ‘Square’ class.
Expected Output:
This is rectangular shape
Square is a rectangle with equal sides

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images

- package lab1; /** * A utility class containing several recursive methods * * <pre> * * For all methods in this API, you are forbidden to use any loops, * String or List based methods such as "contains", or methods that use regular expressions * </pre> * * */ public final class Lab1 { /** * This is empty by design, Lab class cannot be instantiated */ privateLab1(){ // empty by design } /** * Returns the product of a consecutive set of numbers from <code> start </code> * to <code> end </code>. * * @param start is an integer number * @param end is an integer number * @return the product of start * (start + 1) * ..*. + end * @pre. * <code> start </code> and <code> end </code> are small enough to let * this method return an int. This means the return value at most * requires 4 bytes and no overflow would happen. */ publicstaticintproduct(ints,inte){ if(s==e){ returne; }else{ returns*product(s+1,e); } }…arrow_forwardComplete the code and make it run sucessful by fixing errors//MainValidatorA3 public class MainA3 { public static void main(String[] args) { System.out.println("Welcome to the Validation Tester application"); // Int Test System.out.println("Int Test"); ValidatorNumeric intValidator = new ValidatorNumeric("Enter an integer between -100 and 100: ", -100, 100); int num = intValidator.getIntWithinRange(); System.out.println("You entered: " + num + "\n"); // Double Test System.out.println("Double Test"); ValidatorNumeric doubleValidator = new ValidatorNumeric("Enter a double value: "); double dbl = doubleValidator.getDoubleWithinRange(); System.out.println("You entered: " + dbl + "\n"); // Required String Test System.out.println("Required String Test:"); ValidatorString stringValidator = new ValidatorString("Enter a required string: "); String requiredString =…arrow_forwardPlease give me the solutionarrow_forward
- Write all the code within the main method in the TestTruck class below. Implement the following functionality. Construct two Truck objects: one with any make and model you choose and the second object with gas tank capacity 10. If an exception occurs, print the stack trace. Print both Truck objects that were constructed. import java.lang.IllegalArgumentException ; public class TestTruck { public static void main( String[] args ) { // write your code herearrow_forward8) Now use the Rectangle class to complete the following tasks: - Create another object of the Rectangle class named box2 with a width of 100 and height of 50. Note that we are not specifying the x and y position for this Rectangle object. Hint: look at the different constructors) Display the properties of box2 (same as step 7 above). - Call the proper method to move box1 to a new location with x of 20, and y of 20. Call the proper method to change box2's dimension to have a width of 50 and a height of 30. Display the properties of box1 and box2. - Call the proper method to find the smallest intersection of box1 and box2 and store it in reference variable box3. - Calculate and display the area of box3. Hint: call proper methods to get the values of width and height of box3 before calculating the area. Display the properties of box3. 9) Sample output of the program is as follow: Output - 1213 Module2 (run) x run: box1: java.awt. Rectangle [x=10, y=10,width=40,height=30] box2: java.awt.…arrow_forwardWrite the answer in java and read the question carefully here is the lab 12 code : public class lab12daneshelahi { public static void main (String[]args) { CanadianAddress testAddress = new CanadianAddress (); CanadianAddress SMUAddress = new CanadianAddress ("923 Robie St.", "Halifax", "Nova Scotia", "B3H 3C3"); System.out.println ("The testAddress is created as:"); testAddress.printAddress (); System.out.println (); System.out.println ("The SMUAddress is created as:"); SMUAddress.printAddress (); System.out.println (); System.out.println ("The testAddress is equal to SMUAddress? :( " + testAddress.equalAddress (SMUAddress)); System.out.println (); testAddress.setStreetAddress ("923 Robie St."); testAddress.setCity ("Halifax"); testAddress.setProvince ("Nova Scotia"); testAddress.setPostalCode ("B3H 3C3"); System.out.println ("The testAddress is updated as: :D "); testAddress.printAddress (); System.out.println (); System.out.println ("The…arrow_forward
- g) Make a class Class (as in a class of students, not a class as in a class of objects) that stores several Student objects. Store a list of students in an attribute called students. The initializer method should initialize the class as an empty class (no students). Implement a method add_student() that takes an object of class Student and adds it to class. Implement method pass_rate() that determines the fraction of students that pass the class (have a grade of >= 6). python code!!arrow_forward3. Consider the following two classes and write down the output of the following code: public class Test extends G{ void display() { String s = m1(); System.out.println("My name is "+ s); i - 7; j = j + i; } void show(){ System.out.println(" j value is - } public static void main(String args[]) { Test t - new Test(); System.out.println("s value is = t.display(); t.i = 9; System.out.println("i value in the object="+t.i); System.out.println("j value in the object="+t.j); t.show(); } abstract class G{ String s - "Exam"; int i = 10; int j - 5; String m1(){ s = "Java"; return s; abstract void display(); abstract void show( ); } " + j); + t.s);arrow_forwardJava prgm basedarrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





