






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







List the number of different class types in Java; for instance, abstract class. - correct answerYou can declare a class as abstract ,final, private ,protected, nest it in another class and so on. These are some of the comment type of classes
} - correct answerthis() is not the correct statement to call the constructor of base class in derived class. Correct statement is: super()
Default = Only accessible within classes within the same package. Can a class could be a subclass as well as a superclass at the same time? - correct answerYes it can. This is an example of multilevel inheritance Can a Java class inherits more than one class directly? - correct answerNo it cannot Can a static methods access an instance function? Show your answer with working example. - correct answerclass Main { public static void main(String[] args) { ArrayList
// method void print(Instrument inst) { inst.method() ; } } - correct answerBelow code is using interface to create an anonymous class. In an anonymous class, we are able to declare and instantiate the class together. Constructor - correct answerOnly invoked once in the lifecycle of an object Convert the methods in the following class to static methods and make all the necessary correction as a result of the conversion. class Basketball { int home = 0 ; int visitor = 0 ; void setHome(int score) { home = score; } void setVistor(int score) { vistor = score ; } } - correct answerclass Basketball { static int home = 0 ; static int visitor = 0 ; static void setHome(int score) { home = score; } static void setVistor(int score) { visitor = score ; } } // instance variable cannot exist in static method // Correct the problem in the following code. interface Instrument { double goldenRatio = 1.618 ; } class Piano implements Instrument { goldenRatio = 1.61803399 ; - correct answerdouble goldenRatio = 1. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
double width, height, depth; Box(double w, double h, double d) { width = w; height = h; depth = d; } Box() { width = height = depth = 0; } Box(double len) { width = height = depth = len; } } Give an example of a statement where the two operators "?" and ":" are used in unison. - correct answermin = (3>2)?3: ternary operator java Give an example of downcasting. - correct answerAnimal animal = new Dog(); Dog castedDog = (Dog) animal; Give an example of upcasting. - correct answerDog dog = new Dog(); Animal anim = (Animal) dog; Give an example of your own class that cannot be overridden. - correct answerFinal How many interfaces can a class implements? - correct answerIn theory infinite Identify all the superclasses of Piano. class Instrument {} class Piano extends Instrument {} - correct answerInstrument Identify and explain what kind of casting the following snippet code is performing. class Instrument {} class Piano extends Instrument {}
Instrument inst = new Piano() ; //? Piano p = (Piano) inst ; //? - correct answerObject casting from large to small Upcasting object casting from small to large Downcasting Identify and explain what kind of casting the following snippet code is performing. class instrument {} class Piano extends instrumen {} Instrument inst=new Piano (); //? Piano p = (Piano) inst ; //? - correct answerInstrument inst=new Piano (); // this is upcasting Piano p = (Piano) inst ; //this is downcasting Identify and explain what kind of casting this statement is performing. double d = 3.0e8 ; float f = (float) d ; - correct answerprimitive type casting from higher to lower precision Identify the syntax errors class MyClass { } MYClass mc; - correct answerUncompilable source code - cannot find symbol: class MYClass If you were asked to convert the number 3.14159 to 3.0, how would you do it? - correct answerMath.floor(3.14159) implements - correct answerinterface import - correct answerjava.util.* ; JDK - correct answerJava Development Kit Object - correct answerall classes inherit it Object []array - correct answer{new String(), new Random(), new Scanner() } ; OOP - correct answerObject Oriented Programming System.in - correct answerStandard Input Device this - correct answerrefers to the current object
What Java keyword do you use to create a constructor link between a subclass and superclass' constructors? - correct answersuper What must be true for a subclass method to override the superclass method? - correct answerThe method that has the same name and number and type of perimeters and return type as the method that is overridden in the super class but it has more details. When your application starts, which method is invoked first by the JVM? - correct answerpublic static void main(String[] args) Which method is overridden by the subclass? class Instrument { Instrument() { System.out.println("Instrument") ; } void method() { System.out.println("method_1") ; } final void method(String msg) { System.out.println("method_1 " + msg) ; } void method(StringBuffer msg) { // method is overloaded System.out.println( "method_1 " + msg.toString() ) ; } } class Piano extends Instrument { void instrument() { System.out.println("Piano") ; } } - correct answerThere is no overridden method Surprisingly there is overloaded method which is called method Write a class that keeps track of the number of objects created from it. Hint: make the counter a static type. - correct answerclass sample { public static int counter=0; Sample () {
counter++; } } Write a program that sorts a given text order in an StringBuffer object. For instance, if the StringBuffer object value is "dcba", the outcome will be "abcd". - correct answerSystem.out.println(new StringBuffer("dcba").reverse().toString()); Write a program that sorts an array of Customer objects. The Customer class definition is provided below. You are strongly urged that you do solve this problem. class Customer { String name = "" ; String card = "" ; } - correct answer// A Java program to demonstrate use of Comparable import java.io.; import java.util.; // A class 'Movie' that implements Comparable class Movie implements Comparable