Hide all answers
Hide all answers
View all answers
View all answers
Print
Try the Quiz
Answer the following questions about methods in the Java programming language. |
1. When an instance of a class, or object, is specified as a parameter to a method, a reference to the said object is passed to the method. True False
Answer: TrueJava only passes the reference to an object to a method. 2. A static method can refer to any instance variable of the class. True
False
Answer: FalseIt is illegal in Java to refer to instance variables from a static method. 3. All interface methods must be declared as public when implemented in a class. True False
Answer: TrueInterfaces are abstraction of functionality that can be implemented in more than one way when required. The abstraction must be made available as public for the use by other parts of the program.
4. Methods can be overloaded with a difference only in the type of the return variable. True
False
Answer: FalseMethod overloading (having two methods with the same name within a class) requires that parameters to the methods must be different. Food for thought: can you explain why? 5. A method in a class declared as static can only access static class members. True False
Answer: TrueStatic methods may, however, receive objects as parameters. 6. A method in a class declared as static may be invoked simply by using the name of the method alone. True
False
Answer: FalseThis is true when the call is made from within the same class. However, when a static method is invoked from another class, then both the classname as well as the method name must be used to make the call. 7. Each method in a class must have a unique name. True
False
Answer: FalseTwo (or more) methods can have the same name but must have different parameters. For example, "int getHeight(int x)" and "int getHeight(String s)" can be the names of two methods in a class. This is called overloading of methods. 8. Java does not allow a method with the same signature in a subclass, as a method in the super class. True
False
Answer: FalseWhen a method in a subclass had the same signature as a method in a parent class, then it is said to "override" that method. 9. When a method or a variable in a class is declared as private, it can only be accessed by the methods within the same class. True False
Answer: TrueThere are two more access types: members declared as public can be accessed even outside the class, whereas members with default access declarataion can only be accessed within the same package. 10. A method declaration must always contain the access level. True
False
Answer: FalseWhen an access level is not declared, the method gets a package access level by default. 11. A method declared as final can be overridden by subclasses if it is also declared as static. True
False
Answer: FalseA final method cannot be overridden. 12. The return value from a method must always match the declared return type. True False
Answer: TrueWhen a method returns an object, it must be an instance of either the exact class declared as the return type, or its subclass. 13. A method that is overridden in the subclass must retain the same return type and parameter list. True False
Answer: TrueThe overriding method may, however, have a different throws clause as long as it only reduces the number of exceptions being thrown in the overridden method. 14. The access level of an overridden method cannot be changed in a subclass. True
False
Answer: FalseThe overriding method can increase the access to the overridden method. For instance, a protected method may be made public, but not private.
Try the Quiz : Java Programming : Methods : True or False
|