Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ 20 Advanced Java interview questions summary     - Zabbix installation under Linux (Server)

- Depth understanding of the use of Spring Redis (Programming)

- Four safety delete files under Linux tools (Linux)

- Oracle query start with connect by tree (Database)

- MySQL enabled SSD storage (Database)

- CentOS 7.0 Close firewall is enabled by default iptables firewall (Linux)

- To install Xen in Ubuntu 12.04 (Linux)

- HBase Application Development Review and Summary of Series (Database)

- How to use the Vault secure password storage and API key (Linux)

- Plasma 5.4 How to install on Kubuntu 15.04 (Linux)

- socket busy poll of Linux kernel 3.11 to avoid sleep switch (Linux)

- Linux environment variable settings methods and differences (Linux)

- Iptables in Ubuntu (Linux)

- How to set up HTTPS policies for older browsers (Server)

- Thinking in Java study notes - initialization and cleanup (Programming)

- Android start automatically and add and delete a desktop shortcut (Programming)

- Boost notes --Asio - (1) a simple small example of synchronous communication (Programming)

- MySQL management partition table (Database)

- View and modify Linux machine name (Linux)

- Fedora && Arch Linux - the most romantic thing to happen now (Linux)

 
         
  20 Advanced Java interview questions summary
     
  Add Date : 2018-11-21      
         
         
         
  This is an advanced Java interview questions in the first part of the series. This section discusses the variable parameters, assertions, garbage collection initializers, tokenization, date, calendar and so on Java core issues.

Programmer Interview Guide: https: //www.youtube.com/watch v = 0xcgzUdTO5M?
Java Interview Questions collections guide: https: //www.youtube.com/watch v = GnR4hCvEIJQ?
What is a variable parameter?
Assertion use?
When to use assertions?
What is garbage collection?
Use an example to explain the garbage collector?
When to run the garbage collector?
Garbage collection of best practices?
What is the initialization block?
What is a static initializer?
What is an example of initialization block?
What are regular expressions?
What is tokenization?
Examples are given of the token?
How to use the scanner class (Scanner Class) tokenization?
How to add hours (hour) to a date object (Date Objects)?
How to format a Date object?
Java calendar class (Calendar Class) purposes?
How to obtain an instance of the Calendar class in Java?
Explain some of the important class calendar method?
Digital format category (Number Format Class) purposes?

What is a variable parameter?

Variable parameters allow calling different numbers of parameters method. Consider the following examples summation method. This method can be called an int argument, or two int parameters, or multiple parameters int.

 // Int (type) followed ... (three dot's) is syntax of a variable argument.
    public int sum (int ... numbers) {
        // Inside the method a variable argument is similar to an array.
        // Number can be treated as if it is declared as int [] numbers;
        int sum = 0;
        for (int number: numbers) {
            sum + = number;
        }
        return sum;
    }

    public static void main (String [] args) {
        VariableArgumentExamples example = new VariableArgumentExamples ();
        // 3 Arguments
        System.out.println (example.sum (1, 4, 5)); // 10
        // 4 Arguments
        System.out.println (example.sum (1, 4, 5, 20)); // 30
        // 0 Arguments
        System.out.println (example.sum ()); // 0
    }
Assertion use?

Assertion was introduced in Java 1.4 in. It allows you to test hypotheses. If the assertion fails (ie returns false), it will throw AssertionError (if enabled assertion). The basic assertion follows.

private int computerSimpleInterest (int principal, float interest, int years) {
    assert (principal> 0);
    return 100;
}
When to use assertions?

Assertion should not be used to validate data entered into a public method or command line parameters. IllegalArgumentException would be a better choice. In the public process, only to check the cases they simply should not happen with the assertion.

What is garbage collection?

Garbage collection is another name for automatic memory management in Java. Garbage collection is intended for the program to keep as much of the available heap (heap). JVM heap object is no longer needed will be removed from the stack reference.

Use an example to explain the garbage collector?

For example, the following method is called from the function.

void method () {
    Calendar calendar = new GregorianCalendar (2000,10,30);
    System.out.println (calendar);
}
Through the first line of code in a function reference variable calendar, create an object on the heap GregorianCalendar class.

After the function completes execution, a reference variable calendar is no longer valid. Thus, the method does not create a reference to an object.

Recognizing this JVM will be removed from the heap object. This is known as garbage collection.

When to run the garbage collector?

Garbage collection runs (not so bad) and when the JVM whim whim. Possibilities to run garbage collection are:

Heap enough available memory
CPU idle
Garbage collection of best practices?

Programmatically, we can ask (remember this is just a request - not a command) JVM garbage collector to run by calling System.gc () method.

When memory is full, and no objects on the heap can be used for garbage collection, JVM may throw OutOfMemoryException.

The object before it is removed from the garbage heap, run finalize () method. We recommend not to write any code with a finalize () method.

What is the initialization block?

Data blocks are initialized - run when you create or load an object class code.

There are two types of initialization data block:

Static initializer: load code that runs when the class

Initializes an instance: Creates a new object code that runs when

What is a static initializer?

Consider the following example: static codes between {and} is called a static initializer. It only runs when you first load the class. Only static variables can be accessed in a static initializer. Although it created three instances, but the static initialization is run only once.

public class InitializerExamples {
    static int count;
    int i;

    static {
        // This is a static initializers. Run only when Class is first loaded.
        // Only static variables can be accessed
        System.out.println ( "Static Initializer");
        // I = 6; // COMPILER ERROR
        System.out.println ( "Count when Static Initializer is run is" + count);
    }

    public static void main (String [] args) {
        InitializerExamples example = new InitializerExamples ();
        InitializerExamples example2 = new InitializerExamples ();
        InitializerExamples example3 = new InitializerExamples ();
    }
}
Sample output

Static Initializer
Count when Static Initializer is run is 0.
What is an example of initialization block?

Let's look at an example: Each instance of a class is created when an instance initializer code runs.

public class InitializerExamples {
    static int count;
    int i;
    {
        // This is an instance initializers. Run every time an object is created.
        // Static and instance variables can be accessed
        System.out.println ( "Instance Initializer");
        i = 6;
        count = count + 1;
        System.out.println ( "Count when Instance Initializer is run is" + count);
    }

    public static void main (String [] args) {
        InitializerExamples example = new InitializerExamples ();
        InitializerExamples example1 = new InitializerExamples ();
        InitializerExamples example2 = new InitializerExamples ();
    }
}
Sample output

Instance Initializer
      Count when Instance Initializer is run is 1
      Instance Initializer
      Count when Instance Initializer is run is 2
      Instance Initializer
      Count when Instance Initializer is run is 3
What are regular expressions?

Regular expressions allow parsing, scanning, and split the string becomes very easy. Java regular expression commonly used --Patter, Matcher and Scanner classes.

What is tokenization?

Tokenization refers separator will be the basis of a string is divided into several sub-string. For example, the delimiter; split the string ac; bd; def; e four substring ac, bd, def, and e.

Delimiter itself can also be a regular expression is common.

String.split (regex) regex will function as a parameter.

Examples are given of the token?

private static void tokenize (String string, String regex) {
    String [] tokens = string.split (regex);
    System.out.println (Arrays.toString (tokens));
}

tokenize ( "ac; bd; def; e", ";"); // [ac, bd, def, e]
How to use the scanner class (Scanner Class) tokenization?

private static void tokenizeUsingScanner (String string, String regex) {
    Scanner scanner = new Scanner (string);
    scanner.useDelimiter (regex);
    List < String> matches = new ArrayList < String> ();
    while (scanner.hasNext ()) {
        matches.add (scanner.next ());
    }
    System.out.println (matches);
}

tokenizeUsingScanner ( "ac; bd; def; e", ";"); // [ac, bd, def, e]
How to add hours (hour) to a date object (Date Objects)?

Now, let's look at how to add hours to a date object. All operations on the date date are required to date by adding milliseconds to complete. For example, if we want to increase 6 hours, then we need to 6 hours converted into milliseconds. 6 hours = 6 * 60 * 60 * 1000 milliseconds. Consider the following examples.

Date date = new Date ();

// Increase time by 6 hrs
date.setTime (date.getTime () + 6 * 60 * 60 * 1000);
System.out.println (date);

// Decrease time by 6 hrs
date = new Date ();
date.setTime (date.getTime () - 6 * 60 * 60 * 1000);
System.out.println (date);
How to format a Date object?

Formatting Dates must use DateFormat class to complete. Let's look at a few examples.

// Formatting Dates
System.out.println (DateFormat.getInstance (). Format (
        date)); // 10/16/12 5:18 AM
Formatting date with the locale as follows:

System.out.println (DateFormat.getDateInstance (
        DateFormat.FULL, new Locale ( "it", "IT"))
        .format (date)); // marted & ldquo; 16 ottobre 2012

System.out.println (DateFormat.getDateInstance (
        DateFormat.FULL, Locale.ITALIAN)
        .format (date)); // marted & ldquo; 16 ottobre 2012

// This uses default locale US
System.out.println (DateFormat.getDateInstance (
        DateFormat.FULL) .format (date)); // Tuesday, October 16, 2012

System.out.println (DateFormat.getDateInstance ()
        .format (date)); // Oct 16, 2012
System.out.println (DateFormat.getDateInstance (
        DateFormat.SHORT) .format (date)); // 10/16/12
System.out.println (DateFormat.getDateInstance (
        DateFormat.MEDIUM) .format (date)); // Oct 16, 2012

System.out.println (DateFormat.getDateInstance (
        DateFormat.LONG) .format (date)); // October 16, 2012
Java calendar class (Calendar Class) purposes?

Calendar class (Youtube video link - https://www.youtube.com/watch?v=hvnlYbt1ve0) in Java for handling dates. Calendar class provides an easy way to increase and decrease the number of days, months, and the number of years. It also provides a lot of details about the date (day of the year? Which week? Etc.)

How to obtain class instance Calendar (Calendar Class) in Java?

Calendar class can not be created by using the new Calendar. The best way to get an instance of the class is to use the Calendar in the Calendar getInstance () static method.

// Calendar calendar = new Calendar (); // COMPILER ERROR
Calendar calendar = Calendar.getInstance ();
Explain some of the calendar class (Calendar Class) in an important way?

Provided on the Calendar object date (day) July (month) or annual (year) is not difficult. Of the Day, Month or Year Constant call the appropriate set method. The next parameter is the value.

calendar.set (Calendar.DATE, 24);
calendar.set (Calendar.MONTH, 8); // 8 - September
calendar.set (Calendar.YEAR, 2010);
calendar get method

To get information on a specific date - the 24 September 2010. We can use the calendar get method. Parameters have been passed that we want to get value from the calendar - the day or month or year or ...... value you can get from the calendar for example as follows:

System.out.println (calendar.get (Calendar.YEAR)); // 2010
System.out.println (calendar.get (Calendar.MONTH)); // 8
System.out.println (calendar.get (Calendar.DATE)); // 24
System.out.println (calendar.get (Calendar.WEEK_OF_MONTH)); // 4
System.out.println (calendar.get (Calendar.WEEK_OF_YEAR)); // 39
System.out.println (calendar.get (Calendar.DAY_OF_YEAR)); // 267
System.out.println (calendar.getFirstDayOfWeek ()); // 1 -> Calendar.SUNDAY
Digital format category (Number Format Class) purposes?

Number format to format numbers to different regions and in different formats.

Use the default locale digital format

System.out.println (NumberFormat.getInstance () format (321.24f).); // 321.24
Use the locale number format

Use Netherlands locale for formatting numbers:

System.out.println (NumberFormat.getInstance (new Locale ( "nl")) format (4032.3f).); // 4.032,3
Use German locale for formatting numbers:

System.out.println (NumberFormat.getInstance (Locale.GERMANY) .format (4032.3f)); // 4.032,3
Using the default locale formatting currency

System.out.println (NumberFormat.getCurrencyInstance () format (40324.31f).); // $ 40,324.31
Use the locale formatting currency

Use formatting locale Netherlands Currency:

System.out.println (NumberFormat.getCurrencyInstance (new Locale ( "nl")) format (40324.31f).); // 40.324,31?
license

This article, along with any associated source code and files, based on The Code Project Open License (CPOL).
     
         
         
         
  More:      
 
- Oracle Bug caused by the collection of AWR Snapshot fault (Database)
- Circular list of Java programming (Programming)
- Use small network command to check whether PC Security (Linux)
- Wireless LAN security solutions (Linux)
- Ubuntu 15.10 install the latest Arduino IDE 1.6.7 (Linux)
- Applications Linux leap second problem caused (Linux)
- Ubuntu way of decompressing files (Linux)
- To use yum install VLC under CentOS (Linux)
- Linux basic introductory tutorial ---- Software Installation under Linux (Linux)
- Towards Docker (Server)
- Ubuntu 14.04 configure JDK1.8.0_25, switchable version (Linux)
- Use Aptik Battery Monitor monitoring Ubuntu laptop battery usage (Linux)
- Zypper command for SUSE Linux package management (Linux)
- Redis Design and Implementation study notes (Database)
- To establish a secure and reliable Linux operating system (Linux)
- Firewall chapter of Linux server security configuration (Linux)
- C language print various graphic (Programming)
- To assign multiple IP addresses NIC on the CentOS 7 (Linux)
- Zookeeper Rights Management and Quota Management (Server)
- Weld a problem in JDK 8u60 environment (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.