1. Each object of the class contains its own copy of instance variables?
Ans: True
2. Which of the below access specifiers can be used with keyword class
Ans: public
3. What is the output of the following program
public class demo{
public static void main(String[]args){
System.out.println(“hi”+args.length);
}
}
Ans: hi 0
4. What is the output of the following program
Class CharRange{
Public static void main(String[]args){
Char ch=127;
Ch++;
System.out.println(ch+”=”+(int)ch);
}
}
Ans: Garbage character= -128
5. ------------- is a default package in java
Ans: java.lang
6. The instance of operator returns a _ ______value
Ans: boolean
7. Class Test{ Int x =20;
ANS: 3020
8. Public class Test1{ Int i=10;
ANS: 5010
9. Which of the following statements is true about static methods
ANS: All of the above
10. Which of the following is true?
ANS: A class has no constructor, compiler will provide default constructor
11. Will this code compile?
Class Demo{
Demo(char status) {
System.out.println(“Parameterized constructor”);
}
Public static void main(Strings[]args){
Demo ob = new Demo(‘A’);
} }
ANS: This code will as the default constructor is not used in main()
12. Which methods can access to private attributes of a class ?
ANS: only methods those defined in the same class
13. A constructor
ANS: must have the same name as the class it is declared within
Is used to create objects
May be declared private
A,b and c
14. There can be more than one java class is same file if.(choose all that apply)
ANS: Only 1 class has public access modifier and has the same name as the java.file
15.
Public class Exam1{
Private int a;
Public static void main(Strings[]arg){
/* complete the code using the options so that O is printed in console*/
} }
ANS: System.out.println(new Exam1().a);
16. Public class AQuestion{
private int i=j;
private int j=10;
public static void main(String args[]){
System.out.println((new Aquestion()).i);
} }
ANS: Compiler error complaining about forward referencing.
17. What if I write static public void instead of public static void while defining main method
ANS: Program compiles and runs successfully (correct answer)
18. Which is not access modifiers ?
ANS: Final
19. What is the output of the below program ?
class Output {
public static void main(Strings args[]){
int x, y=1; x=10; if(x!=10 &&x/0===0)
System.out.println(y);
Else System.out.println(++y);
} }
ANS: 2
20. A method within a class is only accessible by classes that are defined within the same package as the class of the method one of the following is used to enforce such restriction?
ANS: Do not declare the method with any accessibility modifier.
21. What will be the value of a after execution of the following statements;
int a=10,b=14;
a=((a>b)?(b-a):(b+a))
ANS: 24.0
24.
import java.util.*;
public class Employee{
public String firstName;
public String lastName;
public int age;
public char gender;
public Employee(String firstNameForThisObject,String lastNameForThisObject,char gender){ firstName = firstNameForThisObject;
lastName = lastNameForThisObject;
gender=gender;
} public static void main(String[]arg){
Employee employee = new Employee("firstNameForThisObject","lastNameForThisObject",'M'); System.out.print("first name is:"+employee.firstName);
System.out.print("last name is:"+employee.lastName); System.out.print("age is:"+employee.age); System.out.print("gender is:"+employee.gender);
} }
ANS: first name is:firstNameForThisObjectlast name is:lastNameForThisObjectage is:0ge
nder is: (option d)
1. What is stored in the object obj in the following lines of code? Box obj;
Ans: NULL
2. Which is not an Access Modifier?
Ans: Final
3. Which access modifiers in java is accessed by other classes in the same package
or any subclasses of same package or different package?
Ans: Protected
4. Arrange the access levels in order of decreasing accessibility
Ans: Public Default Private
5. What is not true about default constructor. Choose all that apply.
Ans: Any constructor which is always used to create an object can be called
default constructor. It may or may not have parameters.
6. All the user defined classes in java implicitly have a default constructor even if the
constructor is not defined explicitly
Ans: True
7. Which is not Non-Access Modifiers?
Ans: Private
8.______is a default package in Java
Ans: java.lang
9. Which of the following statement is true about static methods
Ans: All of the above (A static method belongs to the class rather than object
of a class, A static method can be invoked without the need for creating an
instance of a class, A static method can access static data member and can
change the value of it)
10. What will be the value of after execution of the following statements:
int a-10, b-14;
a-((a>b)?(b-a):(b+a))
Ans: 24.0
**11. There can be more than one java class in same file if.(Choose all that apply).
Ans: Only 1 class has public access modifier and has the same name as the
.java file
12. What is the output of the following program
public class demo{
public static void main(String[] args){
System.out.println("hi" +args.length);
} }
Ans: hi 0
13. What if I write static public void instead of public static void while defining main
method?
Ans: Program compiles and runs successfully
14. public class AQuestion{
private int i=j;
private int j=10;
public static void main(String args[]){
System.out.println((new Aquestion()),i);
} }
Ans: Compiler error complaining about forward referencing
15. The static can be used for?
Ans: All the above (variable(also known as class variable), method(also known
as class method), nested class)
**16. Can you say that state of an object is similar to attribute of the class?
Ans: True
17. Which keyword can be used to refer current class instance variable?
Ans: this
18._______is the ability to define methods for derived classes
Ans: Polymorphism
19. Constructors can be private
Ans: True
20. How many constructors can be there in a class?
Ans: any number
21. Arrange the access levels in order of decreasing accessibilty
Ans: public default private
22. Which keyword in Java declares a variable whose value cannot change?
Ans: final
23. What is the output of the below program?
class Number
{
public static void main(String[] args)
{
int num=50;
switch(num>=60)
{
case true: System.out.println("Number lesser than 60!");
break;
case false: System.out.println("Number greater than 60!");
break;
default: System.out.println("Default Case");
break;
}
Ans: boolean can not be converted into int
**24. Importing a package imports the subpackages as well?
Ans: false
25. A constructor
Ans: A, B and C ( a) must have the same name as the class it is declared, b) is
used to create objects, c) may be declared private)
**26. public class Exam1
{ public static void main(String args[])
{ private int a=0;
System.out.println(a++);
}}
What is the output of the above program?
Ans: Compile time error(private & static can’t be in local)
27.Which methods can access to private attributes of a class?
Ans: Only methods those defined in the same class
29. What is the output of the below mentioned code
public class Sample
{
public static void main(String[] args)
{
Demo d=new Demo(20);
System.out.println(d,num);
}
class Demo
{
final int num;
public Demo(int n)
{
num=n;
} }
Ans: 20
30. public class Test1{
static{
System.out.println("Static block");
Ans: True
2. Which of the below access specifiers can be used with keyword class
Ans: public
3. What is the output of the following program
public class demo{
public static void main(String[]args){
System.out.println(“hi”+args.length);
}
}
Ans: hi 0
4. What is the output of the following program
Class CharRange{
Public static void main(String[]args){
Char ch=127;
Ch++;
System.out.println(ch+”=”+(int)ch);
}
}
Ans: Garbage character= -128
5. ------------- is a default package in java
Ans: java.lang
6. The instance of operator returns a _ ______value
Ans: boolean
7. Class Test{ Int x =20;
ANS: 3020
8. Public class Test1{ Int i=10;
ANS: 5010
9. Which of the following statements is true about static methods
ANS: All of the above
10. Which of the following is true?
ANS: A class has no constructor, compiler will provide default constructor
11. Will this code compile?
Class Demo{
Demo(char status) {
System.out.println(“Parameterized constructor”);
}
Public static void main(Strings[]args){
Demo ob = new Demo(‘A’);
} }
ANS: This code will as the default constructor is not used in main()
12. Which methods can access to private attributes of a class ?
ANS: only methods those defined in the same class
13. A constructor
ANS: must have the same name as the class it is declared within
Is used to create objects
May be declared private
A,b and c
14. There can be more than one java class is same file if.(choose all that apply)
ANS: Only 1 class has public access modifier and has the same name as the java.file
15.
Public class Exam1{
Private int a;
Public static void main(Strings[]arg){
/* complete the code using the options so that O is printed in console*/
} }
ANS: System.out.println(new Exam1().a);
16. Public class AQuestion{
private int i=j;
private int j=10;
public static void main(String args[]){
System.out.println((new Aquestion()).i);
} }
ANS: Compiler error complaining about forward referencing.
17. What if I write static public void instead of public static void while defining main method
ANS: Program compiles and runs successfully (correct answer)
18. Which is not access modifiers ?
ANS: Final
19. What is the output of the below program ?
class Output {
public static void main(Strings args[]){
int x, y=1; x=10; if(x!=10 &&x/0===0)
System.out.println(y);
Else System.out.println(++y);
} }
ANS: 2
20. A method within a class is only accessible by classes that are defined within the same package as the class of the method one of the following is used to enforce such restriction?
ANS: Do not declare the method with any accessibility modifier.
21. What will be the value of a after execution of the following statements;
int a=10,b=14;
a=((a>b)?(b-a):(b+a))
ANS: 24.0
24.
import java.util.*;
public class Employee{
public String firstName;
public String lastName;
public int age;
public char gender;
public Employee(String firstNameForThisObject,String lastNameForThisObject,char gender){ firstName = firstNameForThisObject;
lastName = lastNameForThisObject;
gender=gender;
} public static void main(String[]arg){
Employee employee = new Employee("firstNameForThisObject","lastNameForThisObject",'M'); System.out.print("first name is:"+employee.firstName);
System.out.print("last name is:"+employee.lastName); System.out.print("age is:"+employee.age); System.out.print("gender is:"+employee.gender);
} }
ANS: first name is:firstNameForThisObjectlast name is:lastNameForThisObjectage is:0ge
nder is: (option d)
1. What is stored in the object obj in the following lines of code? Box obj;
Ans: NULL
2. Which is not an Access Modifier?
Ans: Final
3. Which access modifiers in java is accessed by other classes in the same package
or any subclasses of same package or different package?
Ans: Protected
4. Arrange the access levels in order of decreasing accessibility
Ans: Public Default Private
5. What is not true about default constructor. Choose all that apply.
Ans: Any constructor which is always used to create an object can be called
default constructor. It may or may not have parameters.
6. All the user defined classes in java implicitly have a default constructor even if the
constructor is not defined explicitly
Ans: True
7. Which is not Non-Access Modifiers?
Ans: Private
8.______is a default package in Java
Ans: java.lang
9. Which of the following statement is true about static methods
Ans: All of the above (A static method belongs to the class rather than object
of a class, A static method can be invoked without the need for creating an
instance of a class, A static method can access static data member and can
change the value of it)
10. What will be the value of after execution of the following statements:
int a-10, b-14;
a-((a>b)?(b-a):(b+a))
Ans: 24.0
**11. There can be more than one java class in same file if.(Choose all that apply).
Ans: Only 1 class has public access modifier and has the same name as the
.java file
12. What is the output of the following program
public class demo{
public static void main(String[] args){
System.out.println("hi" +args.length);
} }
Ans: hi 0
13. What if I write static public void instead of public static void while defining main
method?
Ans: Program compiles and runs successfully
14. public class AQuestion{
private int i=j;
private int j=10;
public static void main(String args[]){
System.out.println((new Aquestion()),i);
} }
Ans: Compiler error complaining about forward referencing
15. The static can be used for?
Ans: All the above (variable(also known as class variable), method(also known
as class method), nested class)
**16. Can you say that state of an object is similar to attribute of the class?
Ans: True
17. Which keyword can be used to refer current class instance variable?
Ans: this
18._______is the ability to define methods for derived classes
Ans: Polymorphism
19. Constructors can be private
Ans: True
20. How many constructors can be there in a class?
Ans: any number
21. Arrange the access levels in order of decreasing accessibilty
Ans: public default private
22. Which keyword in Java declares a variable whose value cannot change?
Ans: final
23. What is the output of the below program?
class Number
{
public static void main(String[] args)
{
int num=50;
switch(num>=60)
{
case true: System.out.println("Number lesser than 60!");
break;
case false: System.out.println("Number greater than 60!");
break;
default: System.out.println("Default Case");
break;
}
Ans: boolean can not be converted into int
**24. Importing a package imports the subpackages as well?
Ans: false
25. A constructor
Ans: A, B and C ( a) must have the same name as the class it is declared, b) is
used to create objects, c) may be declared private)
**26. public class Exam1
{ public static void main(String args[])
{ private int a=0;
System.out.println(a++);
}}
What is the output of the above program?
Ans: Compile time error(private & static can’t be in local)
27.Which methods can access to private attributes of a class?
Ans: Only methods those defined in the same class
29. What is the output of the below mentioned code
public class Sample
{
public static void main(String[] args)
{
Demo d=new Demo(20);
System.out.println(d,num);
}
class Demo
{
final int num;
public Demo(int n)
{
num=n;
} }
Ans: 20
30. public class Test1{
static{
System.out.println("Static block");