JAVA interview questions and answers (1-15)

What is the difference between Q1, inner classes and subclasses?

Ans: Inner class is a class in another class nested. Nested within access class has its class, and it may be an external access to all variables and methods defined in the class.

Subclass inherits from another class called super class of the class. Subclasses can access all public and protected methods and fields of its superclass.

Q2, various access Java classes explain what the operator is?

Ans: In Java, the access specifiers are keywords used before the class name to define access range. Specifier access type classes are:

  • public: classes, methods, fields can be accessed from anywhere.
  • protected: methods, fields can be accessed from the same class, or they belong to the subclass may be, but not accessible from the same type of access to a packet from the outside.
  • Default: methods, fields, from the same class can access a package, rather than from its native package external access.
  • Private: Method, Field can be accessed from the same class to which they belong.

What is the purpose Q3, static methods and static variables are?

Ans: When you need to share the method or variable rather than creating a separate copy for each object across multiple object classes, we use the static keyword for all objects shared method or variable.

Q4, the data package and what is the significance?

Ans: Encapsulation is a concept of object-oriented programming, properties and methods for combining in a single unit.

Package helps programmers to follow a modular approach to software development, because each object has its own set of methods and variables, and independently provide its functionality to other objects. Package also for data hiding purposes.

Q5, what is a singleton? As an example to illustrate its practical usage.

Singleton class java in only one example, and thus all methods and variables that belong to only one example. Singleton class concept in the case of the need to limit the number of classes of objects useful.

Example of a single preferred embodiment scenario is used, because some drivers limitations or other problems, only to establish a connection with a database.

Q6, what is circulating in Java? What three loop that?

Ans: for programming cycles to repeatedly execute a statement or block. Java There are three types of loops:

1) For cycle

for loop statement is repeatedly executed for a given number of times in java. When the programmer to know the number of times the statement is executed, using a For loop.

2)while

When the need to repeat certain statements until the condition is satisfied, using a loop. In the while loop, first check the conditions before executing the statement.

3)do while

While Loop Do While circulating same, but the difference in the execution statement block check conditions. Therefore, in the case of do while loop, the statement is executed at least once.

Q7, what is an infinite loop? How to declare an infinite loop?

Ans: infinite loop run and run indefinitely without any conditions circumstances. By defining interrupt logic statements in the block body, an infinite loop can break.

Infinite loop following statement:

for(;;){
    //要执行的语句

    //添加任何循环破坏逻辑
}

Q8, continue and break statements What is the difference?

Ans: break and continue are two important keywords used in the loop. When using the break keyword in a loop, the loop is immediately interrupted, and when using the continue keyword, the current iteration is interrupted, the cycle will continue for the next iteration.

In the following example, when the counter reaches 4, the cycle is interrupted.

for(counter = 0; counter<10; counter ++){
    System.out.println(counter);
    if(counter == 4){
         break;
    }
}

In the following example, when the counter reaches 4, the next iteration loop to jump to, and skips any statements after continue keywords for the current iteration.

for(counter=0;counter< 10;counter++){
    system.out.println(counter);

    if (counter == 4) {
        continue;
    }
    system.out.println("This will not get printed when counter is 4");
}

Q9, Java in the double and float variables What is the difference?

Ans: In java, float 4 bytes in memory, while Double 8 bytes in memory. Float is a single-precision floating-point decimal number, but is Double precision decimal.

Q10, what is the final keyword in Java? for example.

Ans: In java, use the keyword Final declare a constant. Value can be assigned only once, after the assignment, constant values ​​can not be changed.

In the following example, we declare a constant named const_val and gives avalue:

Private Final int const_val = 100

When a method is declared final, it can not be overridden by classes. This method is faster than any other method, because they are resolved at compile time.

When a class is declared as final, it can not be subclassed. Examples of String, Integer, and other packaging.

Q11, what is the ternary operator? for example.

Ans: ternary operator, also referred to as a conditional operator, according to a boolean value of the evaluation to decide which value to the variable. It is represented as?

In the following example, if the rank is 1, the state is assigned the value "Done", otherwise "Pending".

public class conditionTest {
    public static void main(String args[]) {
        String status;
        int rank = 3;
        status = (rank == 1) ? "Done" : "Pending";
        System.out.println(status);
    }
}

Q12, how to generate random numbers in Java?

Years:

  • Use Math.random () may be generated equal to or greater than 0.1 and less than 1.0 of the random number
  • Use the Random class java.util package

Q13, what is the default Switch case? for example.

Ans: In a switch statement, when no other switch case when the conditions match, the implementation of default. The default is an optional situation. It can only be declared after all other cases are coded switch.

In the following example, when the score is not a 1 or 2 is used by default.

public class switchExample {
    int score = 4;
    public static void main(String args[]) {
        switch (score) {
            case 1:
                system.out.println("Score is 1");
                break;
            case 2:
                system.out.println("Score is 2");
                break;
            default:
                system.out.println("Default Case");
        }
    }
}

Q14, what Java base class for all classes that?

Ans: java.lang.Object

Q15, Java in main () method can return any data?

Ans: In java, main () method does not return any data, because it always returns the type to declare void.


No public concern: "Java confidant" , updated daily Java knowledge Oh, look forward to your arrival!

  • Send "1024" , receive a free 30 classic programming books.
  • Send "Group" , progress with 100,000 programmers.
  • Send "JavaEE combat" and receive "JavaEE combat" series of video tutorials.
  • Send "Fun algorithm" to receive "Fun algorithm" series of video tutorials.
    Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/java-friend/p/11547894.html