Basic Flashcards

1
Q

T or F

In inheritance, private methods are final.

A

True.

All compilers will treat private methods as final. The compiler will not allow any private method to be overridden. Likewise, all compilers will prevent subclasses from overriding final methods.

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

Protected members are accessible within a package and inherited classes outside the package.

T or F

A

True

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

T or F

We cannot override private metods

A

True

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

What’s another word for an object?

A

Instance

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

What’s another word for an instance?

A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
House blueHouse = new House("blue")
House anotherHouse = blueHouse;

System.out.println(blueHouse.getColor);
System.out.println(anotherHouse.getColor);

What color will print? and why?

A

Blue and blue

Because there are two references to the same objects.

https: //www.udemy.com/course/java-the-complete-java-developer-course/learn/lecture/9331916#notes
2: 29

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

T or F

The call super() must be the first statement in each constructor.

A

True

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

Can a constructor have both, a call to super() or this()?

A

False. Can’t have both

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

Can a constructor have both, a call to super() or this()?

A

False. Can’t have both

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

T o F

Can I use the “this” keyword in a getter?

A

True. In the getters there are no parameters, so the “this” keyword is optional.

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

What is the super keyword in Java?

A

Super keyword in java is a reference variable which is referred immediate parent or super class object. In other words, Super keyword is used to access the members of the parent class.

Super keyword point to immediate parent class object. It works with objects only of the parent class.

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

Write some points about super keyword in java?

A
There are some use of super keyword in java.
Super keyword can be used to refer parent class instance variables.
Super keyword can be used to invoke parent class method.
Super keyword can be used to invoke parent class constructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can we access parent class variables in child class by using super keyword?

A

Yes.

class Parent {
    int a = 20;
}
class Child extends Parent {
    int a = 30;
    void show() {
        System.out.println(a);//print child class value of a
        System.out.println(super.a);//print parent class value of a
    }
    public static void main(String args[]) {
        Child c = new Child();
        c.show();
    }
}

Output:30
20

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

Can we use both “this” and “super” in constructor?

A

No, because both this and super should be the first statement.

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

What is the difference between this() and super()?

A

There are some differences between java this() constructor and super() constructor.

Java this()
It is used to access the current class constructor.
It can be used inside another constructor of the same class.
It can be used to remove ambiguity errors when we have data members and local are the same name.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can we use super keyword in the static method of a subclass for calling the parent class method?

A

No, We cannot use super keyword in static methods because it belongs to the immediate parent object and static belongs to the class level.

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

Difference Between this() and super() ?

A

Super keyword is always pointing to base class features and this keyword is always pointing to current class features.

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

T or F

Overloading does not have anything to do with polymorphism.

A

True.

But Java developers often refer to overloading as Compile Time Polymorphism.

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

What is the difference between Overloading andOverriding?

A
  • Overloading is about same function have different signatures.
  • Overriding is about same function, same signature but different classes connected through inheritance.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Is this overriding or overloading?

By extending the parent class, the child class gets all the methods defined in the parent class.

A

Overriding

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

____ means defining a method in a child class that already exists in the parent class with the same signature (same name, same arguments).

A

Overriding

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

Is this overriding or overloading?

The compiler decides which method is going to be called based on the method name, return type and argument list.

A

Overloading

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

Why is overloading very handy?

A

Because it reduces duplicated code and we don’t have to remember multiple method names.

24
Q

Methods will be considered overloaded if both follow the following two rules… what are those two rules?

A
  1. Methods must have the same method name

2. Methods must have different parameters

25
Q

If methods follow the two rules, then they may or may not …

A
  1. Have different return types
  2. Have different access modifiers
  3. Throw different checked or unchecked exceptions
26
Q

Method overriding is also known as _______.

A

Runtime Polymorphism and Dynamic Method Dispatch.

27
Q

Method overriding is also known as Runtime Polymorphism and Dynamic Method Dispatch because …

A

Because the method that is going to be called is decided at runtime by the JVM.

28
Q

When overriding, what annotation is recommended to put above the method definition? and why?

A

@Override

Because the @Override annotation is what the compiler reads and will then show us an error if we don’t follow overriding rules correctly.

29
Q

Can you override static methods? and why?

Y or N

A

No.

No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods.

30
Q

Can you override instance methods?

T or F

A

True

31
Q

Name 3 rules that will be considered methods to be overridden:

A
  1. It must have the same name and same arguments
  2. Return type can be a subclass of the return type in the parent class.
  3. It can’t have a lower access modifier

For example, if the parent method is protected then using private in the child is not allowed, but using public in the child would be allowed.

32
Q

T or F

Only inherited methods can be overridden.

A

True

Methods can be overridden only in child classes.

33
Q

T or F

Constructors and private methods cannot be overridden.

A

True

34
Q

Methods that are final cannot be overridden

A

True

35
Q
T or F
A subclass can use super.methodName() to call the superclass version of an overridden method.
A

True

36
Q

In a static method can you use the “this” keyword?

Y or N

A

No. Because static methods can’t access instance methods and instance variables directly.

The “this” keyword is the current instance of a class.

37
Q

Whenever you see a method that does not use instance variables that method should be declared as a static method.

Y or N

A

Yes.

For example,
Main is a static method and it is called by the JVM when it starts an application.

38
Q

Static methods are called as ClassName.____Name() or ______Name(), only if in the same class.

A

ClassName.methodName() or methodName(), but ONLY if in the same class.

class Calculator {
    public static void printSum(int a, int b) {
       System.out.print("sum= " + (a + b);
    }
{

public class Main {

public static void main(String[] args) {
Calculator.printSum(5, 10);
printHello();
}

public static void printHello() {
    System.out.print("Hello");
} }
39
Q

Do instance methods belong to an instance of a class?

Y or N.

A

Yes.

40
Q

To use an instance method we have to instantiate the class first, usually by sing the ____ keyword.

A

new

41
Q

Can instance methods access instance methods and instance variables directly?

A

Yes

42
Q

Can instance methods access static methods and static variables directly?

A

Yes

class Dog {

public void bark() {
    System.out.print("woof");
} }

public class Main {

   public static void main(String[] args) {
        Dog rex = new Dog();    // create instance
        rex.bark();      // call instance method
   }
}
43
Q

With what word do you declare a static variable?

A

with the keyword “static”

44
Q

Do every instance of a class shares the same static variable?

Y or N

A

Yes

45
Q

If changes are made to a static variable, will all other instances see the effect of the change?

A

Yes.

OUTPUT:
fluffy
fluffy

Explanation: Remember that static variables are shared between instances. In other words, once we change the static variable… all instances will see that change we made.
https://www.udemy.com/course/java-the-complete-java-developer-course/learn/lecture/10576974#notes 2:10

class Dog {

private static String name;

   public Dog(String name) {
        Dog.name = name;
    }

public void printName() {
System.out.print(“name = “ + name);
}
}

public class Main {

   public static void main(String[] args) {
        Dog rex = new Dog("rex");   // create instance rex
        Dog fluffy = new Dog("fluffy");    // instance fluffy
        rex.printName();     // print fluffy
        fluffy.printName();   // print fluffy
    }
}
46
Q

Fact About Static Variables:
Static variables are not used very often but can sometimes be very useful.

Example:
When reading input using Scanner, we will declare scanner a static variable.

A

You are doing great :) keep up the great work!

47
Q

Do instance variables use the “static” keyword?

A

No

48
Q

Name two other ways instance variables are called:
1.
2.

A
  1. fields

2. member variables

49
Q

Do instance variables belong to an instance of a class?

A

Yes

50
Q

Which ones are true?

  1. Every instance has its own copy of an instance variable
  2. Every instance can have a different value (state).
  3. Instance variables represent the state of an instance
A

All 3 are correct

OUTPUT:
rex
fluffy

Explanation: The output will be Rex and Fluffy because we are using instance variables and each instance of the class has its own state or own value.

In other words, each dog has its own copy of the variable name,

class Dog {

private String name;

   public Dog(String name) {
        this.name = name;
    }

public void printName() {
System.out.print(“name = “ + name);
}
}

public class Main {

   public static void main(String[] args) {
        Dog rex = new Dog("rex");   // create instance rex
        Dog fluffy = new Dog("fluffy");    // instance fluffy
        rex.printName();     // print fluffy
        fluffy.printName();   // print fluffy
    }
}
51
Q

What does encapsulation mean?

A

The meaning of Encapsulation is to make sure that “sensitive” data is hidden from users.

52
Q

What are two things you should do to achieve encapsulation?

A

To achieve encapsulation, you must:

  • Declare class variables/attributes as private
  • Provide public get and set methods to access and update the value of a private variable
53
Q

byte, short, int, long, float, double, boolean and char are called:

______ types

A

Primitive types

54
Q

Convert the value of txt to upper case.

String txt = “Hello”;
System.out.println(.***);

A

text.toUpperCase()

55
Q

The class below should not be inherited by other classes. Add the correct modifier:

____ class myClass

A

final

56
Q
True or False
The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.
A

True