Working with Constructors, Methods and Encapsulation Flashcards

1
Q

True/false: An abstract class can have non abstract methods.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True/false: When overriding a method from a super class, you don’t need to consider the access modifier.

A

False

- an overriding method can’t make the overridden method less accessible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True/false: You can have as many classes as you want in one java file.

A
True
- but only one class can be public
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following are true about the “default” constructor:

  1. It is provided by the compiler only if the class and any of its super classes does not define any constructor.
  2. It takes no arguments.
  3. A default constructor is used to return a default value.
  4. To define a default constructor, you must use the default keyword.
  5. It is always public.
A

2
- A constructor does not return any value at all and The access type of a default constructor is same as the access type of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True/false: An overloaded method means a method with the same name and same number and type of arguments exists in the super class and sub class.

A

False.

- This is called an overridden method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

True/false: Static methods don’t have the “this” - this includes static void main()

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
True/false: The following class declaration is valid, assuming two interfaces J and I exists:
abstract class MyIJ implements J , I { }
A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of the following statements are true:

  1. private keyword can never be applied to a class.
  2. synchronized keyword can never be applied to a class.
  3. synchronized keyword may be applied to a non-primitive variable.
  4. final keyword can never be applied to a class.
  5. A final variable can be shadowed in a subclass.
A

2, 5

  • private, protected and public can be applied to a nested class.
  • Synchronized can only be applied to a method or a block.
  • final can be applied to class, variable and methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of these statements are true:

  1. All classes must explicitly define a constructor.
  2. A constructor can be declared private.
  3. A constructor can declare a return value.
  4. A constructor must initialize all the member variables of a class.
  5. A constructor can access the non-static members of a class.
A

2, 5

  • private is used for implementing Singleton Classes (2).
  • All non-final instance variables get default values if not explicitly initialized (4).
  • A constructor is non-static, and so it can access directly both the static and non-static members of the class (5).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following are valid:

  1. public TestClass(int a, int b) { }
  2. public void TestClass(int a) { }
  3. public TestClass(String s);
  4. private TestClass(String s, int a) { }
  5. public TestClass(String s1, String s2) { };
A

1, 4, 5

  • Constructors cannot have empty bodies (i.e. they cannot be abstract) (3)
  • You can apply public, private, protected to a constructor. But not static, final, synchronized, native and abstract. (4)
  • The compiler ignores the extra semi-colon (5).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly