Core Java Question List No1

本文深入探讨了Java平台独立性、虚拟机(JVM)、指针支持与否、基类、多重继承、数组属性、路径与类路径区别、局部与实例变量定义、常量变量声明、main()方法特性、源文件多类声明、包概念、默认导入包、私有类访问限制等关键Java特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. What is the most important feature of Java?

Java is a platform independent language.

  1. What do you mean by platform independence?

Platform independence means that we can write and compile the java code in one platform(e.g windows) and can execute the class in any other supported platform eg(Linux, Solaris, etc).

  1. What is a JVM?

JVM is a Java Virtual Machine which is a run time environment for the compiled Java class files.

  1. Are JVM’s platform independent?
    JVM’s are not platform independent. JVM’s are platform specific run the implementation by the vendor.
  2. What is the difference between JDK and JVM?

JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM  is purely a run time environment and hence you will not be able to compile your source files using a JVM.

  1. What is a pointer and does Java Support pointers?

Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn’t support the usage of pointers.

  1. What is the base class of all classes?

Java.lang.object

  1. Does Java support multiple inheritances?

Java doesn’t support multiple inheritances.

  1. Is Java a pure object oriented language?

Java uses primitive data types and hence is not a pure object oriented language.

  1. Are arrays primitive data types?

In Java, Arrays are objects.

  1. What is difference between Path and ClassPath?

Path and Classpath are operating system level environment variables. Path is used define where the system can find the executable(.exe) files and classpath us used to specify the location .class files.

  1. What are local variables?

Local variables are those which are declared within a block of code like methods. Local variables should be initialized before accessing them. Local variables have the most limited scope. Such a variable is accessible only from the function or block in which it is declared. The local variable’s scope is from the line they are declared on until the closing curly brace of the method or code block within which they are declared.

Every local variable declaration statement is immediately contained by a block. Local variable declaration statements may be intermixed freely with other kinds of statements in the block. A local variable declaration can also appear in the header of a for statement. In this case it is executed in the same manner as if it were part of a local variable declaration statement. Local variables declaration have one and only one final modifier can be used. Local variables are not given default initial values. They must be initialized explicitly before they are used. The scope of a variable determines where it can be used. Syntactically, scope of any variable is the code between the curly braces of where it is declared.

public class HelloApp

{

    public static void main(String[] args)

    {

        String helloMessage;

        helloMessage = "Hello, World!";

        System.out.println(helloMessage);

    }

}

You don’t specify static on a declaration for a local variable. If you do, the compiler generates an error message and refuses to compile your program.

  1. What are instance variables?

Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

  1. How to define a constant variable in Java?

The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can’t be changed also.

Static final int PI=3.14;

  1. Should be main() method be compulsorily declared in all java classes?

No not required main() method should be defined only if the source class is a java application.

  1. What is the return type of the main() method?

Main() method doesn’t return anything hence declared void.

  1. Why is the main() method declared static?

Main() method is called by the JVM even before the instantiation of the class hence it is declared as static.

  1. What is the argument of main() method?

Main() method accepts an array of String object as argument.

  1. Can a main() method be declared final?

Yes. Any inheriting class will not be able to have it’s own default main() method.

  1. Can a main() method be overloaded?

Yes. You can have any number of main() methods with different method signature and implementation in the class.

  1. Does the order of public and static declaration?

No. It doesn’t matter but void should always come before main().

  1.  Can a source file contain more than one class declaration?

Yes. A single source file can contain any number of Class declarations but only one of the class can be declared as public.

public class test {

    public static void main(String[] args){

    }

    class test2{

               

    }

}

  1. What is a package?

Package is a collection of related classes and interfaces. Package declaration should be first statement in a java class.

  1. Which package is imported by default?

java.lang package is imported by default even without a package declaration.

  1. Can a class declared as private be accessed outside it’s package?

Not possible.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值