-
Notifications
You must be signed in to change notification settings - Fork 1
Module 6 Mastery Check
wolvesled edited this page Sep 3, 2013
·
1 revision
- Give this fragment,
class X { private int count;is the following fragment correct?class Y { public static void main(String args[]) { X ob = new X(); ob.count = 10;Answer: it's wrong, you cannot access private member count by ob.count. - An access specifier must____________ a member's declaration. Answer: preceeds
- The complement of a queue is a stack. It uses first-in, last out accessing and is often linked to a stack of plates. The first plate put on the table is the last plate used. Create a stack class called Stack that can hold characters. Call the methods that access the stack push() and pop(). Allow the user to specify the size of the stack when it is created. Keep all other members of the Stack class private. (Hint: you can use Queue class as a model; just change the way the data is accessed.)
- Given this class,
class Test { int a; Test(int i) { a = i; } }write a method called swap() that exchange the contents of the objects referred to by two Test object references. Answer:void swap(Test i, Test j) { int t; t = i.a; i.a = j.a; j.a = t; } - Is the following fragment correct?
class X { int meth(int a, int b) {...} String meth(int a, int b) {...}Answer: it is incorrect, overloading a method should have difference in the parameter list, only return type would not differentiate one method from another for the compiler. - Write a recursive method that displays the contents of a string backwards. Answer:
static void Sreverse(String s, int i) { if(i == s.length() - 1) System.out.print(s.charAt(i)); else { Sreverse(s, i + 1); System.out.print(s.charAt(i)); } } - If all objects of a class need to share the same variable, how must you declare that variable? Answer:
static var-type var-name; - Why might you need to use a static block? Answer: when you want a piece of statements to be executed even before static members, i.e. when class first time loaded that before first time of using the class.
- What is an inner class? Answer: a class defined inside a class or a method. inner class can have access to the members of enclosing class or method, but the enclosing class or method does not have access to the members of nested/inner class.
- To make a member accessible by only other members of its class, what access specifier must be used? Answer: private
- The name of a method plus its parameter list constitutes the method's_______. Answer: signature
- An int argument is passed to a method by using call-by-________. Answer: value
- Create a varargs method called sum() that sums the int values passed to it. Have it return the result. Demonstrate its use.
- Can a varargs method be overloaded? Answer: it depends, when there is not ambiguity in the overloaded method signature, the answer is yes. But if the parameter list confuse the compiler to tell which to use from the other, the answer is no. i.e. void a(int i, int ... v) cannot overload void a(int ... v) but void a(double d, int ... v) can.
- Show an example of an overloaded varargs method that is ambiguous. Answer: when calling a(); if there is void a(int ... v) and void a(double ... v), it creates ambiguity. Or void a(int i, int ... v) is ambiguous to void a(int ... v).