I need some help with some computer science problems I have
I need some help with some computer science problems. I have the questions and the answers listed below. I also bolded what I think the answer is but I just want to double check. If you can also give me a brief explanation, that would be greatly appreciated. Thanks so much in advance!!
1. Given the following code, what is output by the method call, mystery(6 * 8)?
public static void mystery (int x[]) {
System.out.println(\"A\");
}
public static void mystery (int x) {
System.out.println(\"B\");
}
public static void mystery (String x) {
System.out.println(\"C\");
}
A
B (<-bolded, it\'s a little hard to see)
C (<-bolded, it\'s a little hard to see)
CA
CB
-------------------------------------------
Which of the following is true about overloaded methods?
Java cannot use a method\'s return type to tell two overloaded methods apart.
Java cannot use a method\'s parameters to tell two overloaded methods apart.
You can only overload methods that have parameters.
All overloaded methods must have different names.
None of the above
---------------------------------
Consider the following methods:
public static double doStuff(int a) {
return a/2;
}
public static double doStuff(double val) {
return val/10;
}
What is output by the following?
System.out.println(doStuff(5) + doStuff(5.0));
2.00.5
2.50.5
22.5
2.5
5
--------------------------------
Consider the following three classes: Clothing, Socks, and Sweater.
Which of the following keywords allows a child class to access the overridden methods in a parent class?
extends
new
super
this
None of the above
-------------------------------------
Which of the following is true about interfaces:
All methods in an interface must be abstract.
A class can only implement one interface.
An interface can have only non-abstract methods.
Can not contain constants but can have variables.
None of the above.
--------------------------
Solution
1. The answer will be B, the second method will be called, since, the parameters passed while function call is an integer e.g. 6*8 = 48 ==>INTEGER and only 2nd method matches with the datatype of the parameter passed (the first method takes an integer array and the third one takes a String). So, B will be displayed.
2. Java cannot use a method\'s return type to tell two overloaded methods apart. This is true, because Overloading is identified by the compiler by checking the method\'s signature and return type is not a part of method\'s signature.
You can only overload methods that have parameters. You\'re wrong. You can even overload methods that donot have paremeters. ex: the main method can be overloaded by excluding the String[] args parameter.
3. 2.5 There will be not concatenation because are no strings in the print statement, no double quotations. So, the result will be added as a decimal type not as a string.
4. super. super keyword allows derived class to access the super class\' methods and variables. The extends keyword is used to extend an inheritence heirarchy. If you want class B to inherit properies of class A you use extends. e.g. class B extends A {.........}
5. All methods in an interface must be abstract. yes, you\'re right. This is to provide meaning to interfaces. if not true then there would not be any subtle difference between an Abstract Class and an Interface.
Can not contain constants but can have variables. you\'re wrong my friend. There is no such limitation/contract in Interfaces.
HOPE THIS HELPS.
THANKS FOR USING CHEGG


