Part 1 Flashcards

1
Q

What is the minimum that java program should contain, so it can compile by the javac command without any error?

A
The program .java can contain any number of classes, but only one public class to be compiled with javac commnad line.  If the program/file contains public class, the file should be called the same name. For example if the public class is called ExampleName, the file needs to be called the same ExampleName.java
If the program/file doesn’t contain public class, it can be called by any name. 

public class Main { } / java program that can be compiled should contain at least one class (with allowed access modifiers)

It will compiled by: public, default, final, abstract, strictfp
But it will not be compiled with private, protected, static, native and synchronised if it contain only one class

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

What is the minimum that java program should contain to be executable by the java command?

A
Only class that contain main method can be executed by java command. 
When we will run class with java command, it will failed with error if it doesn't contain the main method. 
public class Main {
  public static void main(String[] args) {

}
}

class Main {
  public static void main(String[] args) {

}
}

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

What is required rule to name java class if it is public class?

A

If java class is declared as public it has to be named the same as class:

The file that contain ‘public class Main { }’ should be Main.java

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

What is required rule to name java class if it is not public class?

A

If java class is not declared as public (it has default or final or abstract access modifier), the file can be called by any name.

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

How we can include class from different place into the actual program?

A

If we want to include class from different place into the actual program/file, we can do it by two ways:

  • Reference each class with the whole name with package path in time of usage like
    java. util.ArrayList lits = new java.util.ArrayList();
  • Use import statement in the beginning of the file, rigth after the package statement and before class declaration like: import java.util.ArrayList; In this case we can use simple class name ArrayList in program afterwards.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What kind of import statements we can use in our java program?

A

There are also two ways how to use import statement: explicit and implicit import

Explicit import: use the whole reference package path to the class: import java.util.ArrayList;
Implicit import: use more general reference to whole package that will include all classes inside stated package path: import java.util.*;

But this statement will not include classes in other subpackages, for example java.util package contains regex subpackage that have two classes Pattern and Matcher, if you want include this classes with implicit import statement, you need to declare like: import java.util.regex.*;
Explicitly it would be: import java.util.regex.Pattern;

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

How we can include the classes that are part of subpackage of the more general package? Show the example of such packages.

A

Package statment doesn’t include reference to its packages, therefore we need include each subpackage of that package separately.

For example java.util package contains regex subpackage that have two classes Pattern and Matcher, if you want include this classes with implicit import statement, you need to declare like: import java.util.regex.*;
Explicitly it would be: import java.util.regex.Pattern;

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

Why we should use rather explicit import over the implicit import?

A
The best way is to use explicit import, because we can overuse the implicit imports and it could cause umbiguity of class reference: 
For example import java.util.*; and import java.sql.*; both contains class named Date, and if we will reference this class as Date in program, it will fail because it will not know what class Date it should use.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How compiler will look for classes based on import statement?

A

There are some order how java compiler looking for classes:

  1. it will look for based on explicit import statement
  2. it will take classes from the same directory
  3. it will look for based on implicit import statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between general import and static import?

A

normal imports start with: Math; util.; // we are looking for classes
static imports start with: Math.sqrt; Math.
; // we are looking for members

We can use normal imports to import classes and interfaces of a package. Whenever we are using normal import we can access class and interface directly by their short name. 
We can use static import to import static members of a particular class. 

import static java.lang.Math.*; is correct
import java.lang.Math; is correct
import static java.lang.Math; is incorrect
import static java.lang.Math.sqrt; is correct

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

How compiler will look for static members that should be included to program? (precedence order)

A

While resolving static members compiler will give the precedence in the following order:

  1. Current class static members
  2. Explicit static import
  3. Implicit static import
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why we are using the package statement in java program (name 4 objections at least)?

A

It is an encapsulation mechanism to group related classes and interfaces into a single module.

The main objectives of packages are:

  • to resolve name conflicts
  • to improve modularity of the application
  • to provide security
  • there is one universally accepted naming convention that is to use internet domain name in reverse
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Where the compiler will create a .class file if the compiled file doesn’t contain the package statement in it? So we will use single command javac without any argument.

A
If you will type only javac Test.java the compiler will create class file inside the current working directory. 
But if you want to create class file inside the same package as for program/file, use the javac  -d argument:
javac -d . Test.java
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When you want to create class file inside the same package as the program has stated with package statement in it, what command line argument will you use?

A
But if you want to create class file inside the same package as for program/file, use the javac  -d argument:
javac -d . Test.java 
it will go to package stated in program file with package statement 
. [dot] means current working directory

You can use also direct statement to path, for example use E: root
javac -d E: Test.java

Then if you want to execute/run it with java command, use whole package path for execution:
java com.durgasoft.ocja.Test

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

How many package statements can program file .java contains and where you should place it inside that program file?

A

0 or 1 package is allowed for one program/file
First (at the beginning) statement in file should be package, if it is stated after import statement compilation will failed. Order is important: package -> import -> class|interface|enum

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

In Windows system, what delimiter will you use to choose the classpath directory, and what delimiter is used for path. Provide example.

A

On Windows system classpath directories are delimited with backward slashes \ and paths are delimited with semicolons ;
javac -d classes -cp 3rdPartyCode\classes;.GreetingsUniverse.java
javaw MainClass or javaw.exe MainClass // exclude command window

17
Q

In POSIX systems, what delimiter will you use to choose the classpath directory, and what delimiter is used for path. Provide example.

A

On POSIX systems classpath directories are delimited with forward slashes / and paths are delimited with colons :
-classpath .:/dir_a/classes_a/:/dir_b/classes_b
java MainClass & // exlude command window

18
Q

What is the root node of the file system?

A

Most file systems in use today store files in a tree (or hierarchical) structure. At the top of the tree is one (or more) root nodes. Under the root node, there are files and directories (folders). Each directory can contain files and subdirectories, which in turn can contain files and subdirectories and so on.

19
Q

What OS system have support for multiple root nodes - how it is denoted?

A

Microsoft Windows supports multiple root nodes.
C:\
D:\

20
Q

How many root nodes can Solaris OS support and how it is denoted?

A

The Solaris OS supports a single root node, whic is denoted by forward slash character: /

21
Q

How we separate the directory names in Windows? - what delimiter is used?

A

The character used to separate the directory names is specific to the file system. Microsoft Windows uses back slash \
C:\home\sally\statusReport.txt

22
Q

How we separate the directory names in Solaris OS? - what delimiter is used?

A

The Solaris OS uses forward slash / to separate directories names.
/home/sally/statusReport.txt

23
Q

What is the absolute path?

A

An absolute path always contains the root element and the complete directory list required to locate the file. All of the information needed to locate the file is contained in the path string.
C:\home\sally\statusReport.txt

24
Q

What is the relative path?

A

A relative path needs to be combined with another path in order to access a file. For example joe/foo is a relative path. Without more information, a program cannot reliably locate joe/foo directory in the file system.

25
Q

What are symbolic links in file system?

A

Some files systems also support the notion of symbolic links. A symbolic link is also reffered as a symlink or soft link.
A symbolic link is a special file that serves as a reference to another file.
The file or directory being pointed to is called the target of the link.

26
Q

What does the phrase resolving a link mean?

A

The phrase resolving a link means to substitute the actual location in the file system for the symbolic link. For example resolving logFile yields to
dir\logs\HomeLogFile.txt