Learn Java Tutorials Questionwise,Java Tutorials- Questions And Answers -2

No comments
This is the second article in the series of learning java in the form of question answers form.In this article we have explained some more basic but important concepts of core java in questions -answers form.We hope you will like it.

Q.1 How many classes in one source code file we can declare and how many of them we can declare as public?

Answer-We can declare any number of non-public classes in a single source code file in java, but there should be only one public class in the source-code file.

Q.2 What should be the name of the source code file in java?

Answer-If we have more than one class declared in a source code file then, the name of the file should be as same as the name of the public class in the source code file.

Q.3 What should be the location of the import statements, package names and comments in a source code file in java?

Answer-In a java source code file , the very line should be the package declaration, and after then all the import declaration statements should be written if there are any and after that we should declare the class.

Q.4 How to declare a class in java?

Answer-Syntex for declaring a class in java-


 class  {
//code inside the class
}

For example-
public class Apple{

}


In the above code a class is declared with name Apple and access modifier public.

Q.5 What are the type of modifiers in java?

Answer-There are two type of modifiers in java-

  • Access modifiers(public,private,protected)

  • Non access modifiers(final,static,abstract,stricfp..)

Q.6 What is the meaning of class access or accessing a class?

Answer-If we say that we can access a class that means we can do the following things with that class-

  • we can create an instance of that class from any other class which has access to that class.

  • we can extend that class

  • We can override its methods depending on the access modifiers of the methods.

Q.7 Explain default access modifier for a class in java?

Answer-if we do not declare any access modifier before a class then it comes under default access, which is a package level access modifier,that means the class is accessible only to the classes within the same package.

Q.8 What is the meaning of import statements in java?

Answer-import statements are used in class for importing the classes from a different package so that they can be referred in the class with just class name and there is no need to write the class name with the full package name to refer it.

Q.9 How to import all the classes from a package in java?

Answer-To import all the classes from a package we use * at the end of the package name.
For example- java.util.*
It means all the classes inside the util package has been imported.

Q.10 Can we refer to a classes from another package without using import statement?

Answer-yes we can refer a class from another package in java without using import statements, we just need to write the class name with full package name.

Q.11 What is a public class in java?

Answer-A public class can be accessed by all the classes in the java universe without any restriction,from any package.

Q.12 What are the access and non access modifiers we can use for a class in java?

Answer-access modifiers used for classes in java are- public,default
non-access modifiers used for classes in java are-final,abstract,strictfp

Q.13 Can we declare a class with using modifiers final and abstract together? why?

Answer-no we can never use a class with using both modifiers final and abstract together,because they are used to define totally opposite meaning, An abstract class can never be instantiated so it must be extended in order touse it while a final class can never be extended by any other class.

Q.14 What is a final class in java?

Answer-A final class in java can never be subclassed ie no class can extend a final class. We should only declare a class final only if we are sure that we need no specific subclass for that class.

Q.15 What is an abstract class in java?

Answer-An abstract class in java can never be instantiated ie we can not create objects of instances of an abstract class, the only way to use an abstract class is to extend it.

Q.16 Can we declare an abstract method in a non abstract class?

Answer-No we cannot declare any abstract method inside a non-abstract class,if a class has even a single abstract method then the whole class need to be declared as abstract.

Q.17 Can we declare an non-abstract method in an abstract class?

Answer-We can declare any number of non-abstract methods inside a abstract class.


This was another article in which we have explained some more basic concepts of java programming in the question answer form.I hope you enjoyed learning it in question-answer form.If you have any questions regarding the topics explained above then feel free to ask in the comments.
thanks..

No comments :

Post a Comment