Java Basics Flashcards

1
Q

Is the following syntax valid?

int x=10, int y;

A

No.

It must be written either:

int x=10; int y;

or

int x=10, y;

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

Must a java file have a public class?

A

No.

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

If a class (or enum) is public then where must it be defined?

A

In a file by the same name.

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

Can there be more than one public class (or enum) per file?

A

No.

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

Is final a valid modifier for a main method?

public final static void main (String[ ] args) { }

A

Yes.

Final means this cannot be overridden. Making this static method final prevents subclasses from implementing the same (hidden) static method.

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

What does Object class’s toString method do?

A

It prints the name of the class + the @ sign + the object’s hash code.

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

If you pass the name of an array to a print/println method, what happens?

A

It will print the symbol ` [ ` + the Element Type encoding + the ` @ ` sign + the hash code.

E.g.: [LmyArray;@

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

If you do not import a given package, how can you still access its public methods?

A

Use the fully qualified class name.

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

T/F: Every class belongs to a package.

A

True.

Even if you omit the package statement, the class belongs to the unnamed package.

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

The following are the complete contents of TestClass.java file. Which packages are automatically imported?

class TestClass{
   public static void main(String[] args){
     System.out.println("hello");
  }
}
A

java.lang

and

“The package with no name”

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

Does a main method necessarily need to take an array of strings as an argument if it is to run from the command line?

A

Yes.

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

A class or interface can be referred to by using a fully qualified name or a simple name. What must happen to be able to use the simple name?

A

The class must be in the same package or imported.

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

Does Java support multiple inheritance of types?

A

Yes.

Interfaces, classes and enums are all “types”. Since a class can implement multiple interfaces, Java does support multiple inheritance of types.

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

Does Java support multiple inheritance of state?

A

No.

State is represented by instance fields, which only a class can have.

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

What is encapsulation?

A

The technique used to package the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

It helps make sure that clients have no accidental dependence on the choice of representation

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

When a program is called from the command line with no arguments, what is the length of the args array?

A

Zero.

17
Q

Name two benefits of polymorphism.

A

It makes code more reusable.

It makes code more dynamic.

18
Q

Is the following syntax valid?

import static com.foo.*

(foo is a package)

A

No. You cannot import a package statically.

19
Q

Can you import a package statically?

A

No.

You can only import static members of a class statically.

20
Q

What is the syntax for importing static fields?

A

import static…*

or

import static ..fieldname>;

21
Q

DID YOU READ THE QUESTION CAREFULLY?

A

Yes.

22
Q

Is the this reference available with a static method?

A

No.

23
Q

Does each object of a class have its own copy of each non-static member variable?

A

Yes.

24
Q

In an instance method, is calling this.foo() the same as calling foo( ) ?

A

Yes, the this keyword is a assigned to the current object automatically by the JVM.