Question : Which three statements describe the object-oriented features of the Java language?
A. objects cannot be reused. B. A subclass can inherit from a superclass. C. objects can share behaviors with other objects. D. A package must contain more than one class. E. object is the root class of all other objects. F. A main method must be declared in every class.
1. A,B,D 2. B,C,E 3. A,C,F 4. B,C,F
Correct Answer : 2 Explanation: once, you create an object of a class and you can use it as many time as it is needed or until destroyed.
public class Test{ public void method1(); public void method2(); public static void main(String args[]){ Test t1 = new Test(); t1.method1(); t1.method2(); } }
We have used same object t1 twice.
You can call method of another objects, if you have refrence to that object. Hence, yes behaviors can be shared. No it is not necessary to package have a class. Package can contain 0 to n number of classes.
Question : Given:
And given the commands: javac Test.Java Java Test Hello What is the result? 1. Success 2. Failure 3. Compilation fails. 4. Anexception is thrown at runtime
Correct Answer : 2 Explanation: Here, we have used ternary operators. ("Hello".equals("Hello")) ? false : true;
Both the string are same , hence condition is true. Which will return false. Hence, code will go into else block which will print "Failure"
Program will run perfectly and prints the "Failure"
Question : Given the code fragment
public static void main(String[] args) { double discount = 0; int qty = Integer.parseInt(args[0]); //line n1 }
And given the requirements: - If the value of the qty variable is greater than or equal to 90, discount = 0.5 - If the value of the qty variable is between 80 and 90, discount = 0.2 Which two code fragments can be independently placed at line n1 to meet the requirements?
Exp : A program can use exceptions to indicate that an error occurred. To throw an exception, use the throw statement and provide it with an exception object - a descendant of Throwable - to provide information about the specific error that occurred. A method that throws an uncaught, checked exception must include a throws clause in its declaration.
A program can catch exceptions by using a combination of the try, catch, and finally blocks.
The try block identifies a block of code in which an exception can occur. The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception. The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block. The try statement should contain at least one catch block or a finally block and may have multiple catch blocks.
The class of the exception object indicates the type of exception thrown. The exception object can contain further information about the error, including an error message. With exception chaining, an exception can point to the exception that caused it, which can in turn point to the exception that caused it, and so on. Advantage 1: Separating Error-Handling Code from "Regular" Code Advantage 2: Propagating Errors Up the Call Stack Advantage 3: Grouping and Differentiating Error Types
Question : Given the code fragment: What is the result?
1. A B C Work done 2. A B C D Work done 3. A Work done 4. Compilation fails