1.What is the purpose of a statement block?
Ans: A statement block is used to organize a sequence of statements as a single statement group
2.What is a Java package and how is it used?
Ans: A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.
3.What modifiers may be used with a top-level class?
Ans: A top-level class may be public, abstract, or final.
4.What are wrapped classes?
Ans: Wrapped classes are classes that allow primitive types to be accessed as objects.
5.Describe the wrapper classes in Java ?
Ans: Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
Following are the lists of the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void
6.What are different types of inner classes ?
Ans: Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes, which became available with Java 1.1, are four types:
1.Static member classes: A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.
2.Member Classes: A member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.
3.Local Classes: Local Classes declared within a block of code and these classes are visible only within the block.
4.Anonymous Classes: These type of classes does not have any name and its like a local class
7.Can a class be declared as static?
Ans: No a class cannot be defined as static. Only a method, a variable or a block of code can be declared as static.
8.When will you define a method as static?
Ans: When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static.
9.What are the restriction imposed on a static method or a static block of code?
Ans: A static method should not refer to instance variables without creating an instance and cannot use "this" operator to refer the instance.
10.I want to print "Hello" even before main() is executed. How will you acheive that?
Ans: Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main() method. And it will be executed only once.
Ans: A statement block is used to organize a sequence of statements as a single statement group
2.What is a Java package and how is it used?
Ans: A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.
3.What modifiers may be used with a top-level class?
Ans: A top-level class may be public, abstract, or final.
4.What are wrapped classes?
Ans: Wrapped classes are classes that allow primitive types to be accessed as objects.
5.Describe the wrapper classes in Java ?
Ans: Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
Following are the lists of the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void
6.What are different types of inner classes ?
Ans: Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes, which became available with Java 1.1, are four types:
1.Static member classes: A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.
2.Member Classes: A member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.
3.Local Classes: Local Classes declared within a block of code and these classes are visible only within the block.
4.Anonymous Classes: These type of classes does not have any name and its like a local class
7.Can a class be declared as static?
Ans: No a class cannot be defined as static. Only a method, a variable or a block of code can be declared as static.
8.When will you define a method as static?
Ans: When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static.
9.What are the restriction imposed on a static method or a static block of code?
Ans: A static method should not refer to instance variables without creating an instance and cannot use "this" operator to refer the instance.
10.I want to print "Hello" even before main() is executed. How will you acheive that?
Ans: Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main() method. And it will be executed only once.
Previous | Next |
:: Click the links below for similar type Questions and answers ::
0 comments:
Post a Comment