B. General Questions about Java Flashcards

1
Q

What are wrapper classes?

A

A wrapper class converts java primitives into objects. So a primitive wrapper class is a wrapper class that encapsulates, hides, or wraps data types from the eight primitive data types so that these can be used to create instantiated objects with methods in another class or in other classes. The primitive wrapper classes are found in the Java API.

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

What is casting? and the types of casting with details?

A

Type casting is when you assign a value of one primitive data type to another type.

Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can you use a primitive type when using Arraylist? Why or why not?

A

No. Because the list can only store objects and primitive types are not objects. Unless we cast them using the wrapper class.

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

What is the difference between valueOf() and parseFloat()?

A

Float.parseFloat(String) converts the string to primitive float.
valueOf(String) - returns the Float object initialized with the value provided.

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

Does Java support multiple inheritance?

A

No, Java does not support multiple inheritance. Each class can extend only one class but is able to implement more than one interfaces.

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

Do you use “implement” or “extend” from one class to another?

A

Extend

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

If the parentclass has a protected variable, can the subclass access it? Why or why not?

A

Yes. Because subclasses can acess public and protected variables, but not private.

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

Why and when would you use inheritance?

A

It is useful for code reusability. To reuse attributes and methods of an existing class when creating a new class.

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

What is inheritance?

A

Inheritance can be defined as the process where one class (called the subclass or child class) acquires the properties (methods and fields) of another class (called the superclass or parent class).

Inheritance provides reusability of code and can be used to add additional features to an existing class, without modifying it.

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

What does the word Polymorphism mean?

A

Polymorphism means “many forms”.

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

What is Polymorphism?

A

Polymorphism is the ability of programming languages to present the same inferface for different underlying data types. A plymorphic type is a type whose operation can also be applied to values of some other type.

In other words, when using inheritance, we inherit attributes and methods from the superclass/parent class. Polymorphism uses those attribues and/or methods to perform different tasks. This allows the developer to perform a single action in different ways.

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

What are the types of Polymorphism?

A

There are two types of Polymorphism in Java:
• Compile-time polymorphism (Static binding) – Method overloading
• Runtime polymorphism (Dynamic binding) – Method overriding
We can perform polymorphism by Method Overloading and Method Overriding.

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

What is the keyword used to allow one class to inherit the properties of another class?

A

extends

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

Are constructors members of a class?

A

No. Constructors are not members, so they are not inherited by subclasses. But the subclass automatically acquires the default constructor of the superclass.

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

What keyword do you use to call a parameterized constructor of the superclass?

A

super(values);

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

What word do you use when a class inherits the properties of an interface?

A

Implements

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

Can interfaces be extended by a class?

A

No.

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

public interface Animal {
}

public class Mammal implements Animal {
}
public class Dog extends Mammal {
}

In this example, what keyword can you use to check wheter Mammal is actually an Animal, and dog is actually an Animal?

A

instancef

   public static void main(String args[]) {
      Mammal m = new Mammal();
      Dog d = new Dog();
  System.out.println(m instanceof Animal);
  System.out.println(d instanceof Mammal);
  System.out.println(d instanceof Animal);    }

Output:
true
true
true

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

HAS-A relationship

These relationships are mainly based on the _____.

A

Usage. This determines whether a certain class HAS-A certain thing

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

Name all the members that a subclass inherits from its superclass?

A

Fields, methods and nested classes.

Extra info: Constructors are not members, but can be invoked from the subclass.

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

In Java, an Is-A relationship depends on _____

A

Inheritance.

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

In Java, a Has-A relationship is also known as ______

A

Composition

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

Can a class extend more than one class? Why or why not?

A

No.

However, a class can implement one or more interfaces.

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

How would you define polymorphisim in simple words?

A

We can define polymorphism as the ability of a message to be displayed in more than one form.

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

What is method overloading?

A

Method overloading is the compile-time polymorphism where two or more methods share the same name, with different parameters or signature and different return type.

26
Q

What is method overriding?

A

Method overriding is the runtime polymorphism having same method with same parameters or signature, but associated in different classes.

27
Q

Why do we need Method Overloading ?

A

If we need to do same kind of the operation with different ways i.e. for different inputs. It is hard to find many different meaningful names for single action.

28
Q

Method overloading can be done by changing:

A

The number of parameters in two methods.
The data types of the parameters of methods.
The Order of the parameters of methods.

29
Q
  1. Which of the below are reserved keyword in Java?

A. array
B. goto
C. null
D. int

A

The goto and int are reserved keywords in java. array and null are not keywords in java.

30
Q

What’s the difference between a “class variable” and an “instance variable” in Java?

A

Class variables are declared with the keyword static.

Instance variables are declared without the static keyword.

31
Q

Are class variables stored inside the class memory or inside the object memory?

A

class memory

32
Q

Are instance variables stored inside the class memory or inside the object memory?

A

object memory.

Extra points: Each object will have its own copy of instance variables.

33
Q

Can we extend “final” classes in Java?

A

No

34
Q

True or false.

The “extend” keyword is used to extend a class in java.

A

False.

You can use the “extend” keyword to extend two interfaces.

35
Q

What will be the output of below program?

public class Test {
	public static void main(String[] args) {
		Super s = new Subclass();
		s.foo();
	}
}
class Super {
	void foo() {
		System.out.println("Super");
	}
}
class Subclass extends Super {
	static void foo() {
		System.out.println("Subclass");
	}
}

A. Compile time error
B. Super
C. Subclass
D. Runtime error

A

Correct Answer: A

Subclass foo() method can’t be static, it will give compile time error. This static method cannot hide the instance method from Super.

Remember… Instance variables are declared without the static keyword.

36
Q

Do instance variables have default values?

A

Yes.

For numbers, the default value is 0.
For boolean it is false, and
for Objects it is null.

37
Q

What is the default value of a boolean variable?

A

False

38
Q

What is the default value for numbers?

A

0

39
Q

What is the default value of Object variable?

A

null.

The “object” variable has default value of null if defined as an instance/static variable.

40
Q

Values can be assigned during the ______ or within the ______.

A

declaration

constructor

41
Q

What is an applet?

A

A java applet is program that can be included in a HTML page and be executed in a java enabled client browser. Applets are used for creating dynamic and interactive web applications.

42
Q

What is the size of double variable?

A

64 bit

43
Q

What is the default value of byte variable?

A

0

44
Q

True or False

A class always has a default constructor.

A

false

45
Q

What is an applet?

A

An applet is a Java program that runs in a Web browser.

46
Q

What is the default value of String variable?

A

null

47
Q

What of the following is the default value of a local variable?

A - null

B - 0

C - Depends upon the type of variable

D - Not assigned

A

Answer : D
Explaination
Local variables are not assigned any value by default.

48
Q

What is the default value of char variable?

A

‘\u0000’

char variable has default value of ‘\u0000’ if defined as an instance/static variable.

49
Q

True or false

Can a constructor be made private?

A

True

Yes, class with private constructor can not instantiated using new keyword.

50
Q

What is an Interface?

A - An interface is a collection of abstract methods.

B - Interface is an abstract class.

C - Interface is an concrete class.

D - None of the above.

A

Answer : C
Explaination
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

51
Q

Can constructor be made final?

A

No, this is not possible.

52
Q

What is a concrete class?

A

A concrete class is a class that has an implementation for all of its methods.

53
Q

An abstract class is a ____ class that cannot be used to create objects.

To access it, it must be ____ from another class.

A

restricted

inherited

54
Q

Both abstract class and interface are used for ______.

A

abstraction

55
Q

True or False

A final method in a superclass can be overwritreen.

A

False.

A final method in a superclass cannot be overridden in a subclass.

This guarantees that the final method implementation will be used by all direct and indirect subclasses in the hierarchy.

56
Q

Yes or no

Are abstract classes declared using the abstract modifier?

A

Yes

57
Q

Yes or no

Can concrete classes be declared using abstract modifier?

A

no

58
Q

True of False

An abstract class may or may not contain abstract methods.

A

True

59
Q

True or False

A concrete class cannot contain an abstract method.

A

True

60
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