Java: Test Java1
Summary
- Java căn bản (Variables and Operators, Decision – Making and Iterations)
- Các phần liên quan đến class: tạo class, constructor, set/get, inheritance, override, overload
- Package & Access Specifier
- Interface
- Nhập dữ liệu từ bàn phím (lớp Scanner)
- Mảng
- Chuỗi
- Exception
Question Review
Introduction to java
1. Which is represent the behavior of an object
- Class
- Method đ úng
- Object
- Field
2. Which is an instance of a class
- Class
- Method
- Object đ úng
- Field
3. Which is represent the state of Object?.
- Class
- Object
- Method
- Field đúng
4. Which is describes an entity?
- Class
- Object đúng
- Method
- Field
- Java APIs contain libraries of classes and other software components as interfaces
- True đúng
- False
5. JVM serves as the intermediary between a java program and a host computer
- True đúng
- False
6. The java JDK does not include APIs
- True
- False đung
7. JVM is not available on different operating systems
- True
- False đúng
8. Which is the feature of java provides security control
- Distributed
- Object – Oriented
- Secure đúng
- Multithreading
9. Which is the feature of java provides support for performing many tasks simultaneously
- Distributed
- Platform – independent
- Object – Oriented
- Multithreading
10. Which is the feature of java can run on any machine
- Distributed
- Platform – independent đ úng
- Object – Oriented
- Multithreading
11. The javac tool invokes the java compiler
- True đ úng
- False
- The java tool invokes the java interpreter
- True
- False đ úng
12. The .class files contain bytecodes.
- True đ úng
- False
- The java interpreter compiles code
- True đ úng
- False
13. A multi-line comment starts with forward slash and a hash (/#)
- True
- False đ úng
- A java doc comment ends with double asterisk and forward slash (**/)
- True
- False đ úng
14. Comment appears within delimiters
- True đ úng
- False
Variables and Operators
1. Variable need not to initialized when declared
- True đ úng
- False
2. Variable names $bank_account and _1account are valid.
- True đ úng
- False
3. Only one variable can be declared in a statement at a time
- True
- False đung
4. Variable can be declared to store any type of value
- True đ úng
- False
5. You are trying to use the form at specifiers to display the output:
“100*55.0=5500.000”, “100*55.0=5500.000000”,”100*55.0=5.50e+3” and “100*55.0=0”
Which of the following codes will help you achieve this
a. double result = 100*55.0;
int =0;
System.out.printf(“100 * 55.0 = %1.3f %n”, result);
System.out.printf(“100 * 55.0 = %5f %n”, result);
System.out.printf(“100 * 55.0 = %1.5f %n”, result);
System.out.printf(“100 * 55.0 = %d %n”, q);
b. double result = 100*55.0;
int =0;
System.out.printf(“100 * 55.0 = %1.3f %n”, result);
System.out.printf(“100 * 55.0 = %5f %n”, result);
System.out.printf(“100 * 55.0 = %1.2e %n”, result); đ úng
System.out.printf(“100 * 55.0 = %d %n”, q);
c. double result = 100*55.0;
int =0;
System.out.printf(“100 * 55.0 = %09.3f %n”, result);
System.out.printf(“100 * 55.0 = %5f %n”, result);
System.out.printf(“100 * 55.0 = %f %n”, result);
System.out.printf(“100 * 55.0 = %d %n”, q);
d. double result = 100*55.0;
int =0;
System.out.printf(“100 * 55.0 = %3f %n”, result);
System.out.printf(“100 * 55.0 = %5f %n”, result);
System.out.printf(“100 * 55.0 = %1.2e %n”, result);
System.out.printf(“100 * 55.0 = %d %n”, q);
6. The nextDouble() method of Scanner class return the next token as a byte value
- True
- False đ úng
7. The nextByte() method of Scanner class return the next token as a byte value
- True đ úng
- False
8. You are using various operators to solve an expression. Which of the following statements will display the output as “true”
a. int a=5, b=4, c=3, d=2, e=1;
System.out.println((a+b/5.0-c*d+1*e>(a*b)&&(b<d));
b. int a=5, b=4, c=3, d=2, e=1;
System.out.println((a+b/5.0+c*d+1*e>(a/b)&&(b>d)); đ úng
c. int a=5, b=4, c=3, d=2, e=1;
System.out.println((a+b+5.0-c*d+1*e<(a*b)&&(b>d));
d. int a=5, b=4, c=3, d=2, e=1;
System.out.println((a+b/5.0-c*d*1*e>(a*b)&&(b<d));
9. you want the output to be displayed as “Value of floatTest after conversion: 1.0”, “Value of dblTest after conversion: 1.0” and “Value of sum after conversion: 2”. Can you arrange the steps in sequences to achieve the same
a. System.out.println(“dblTest=”+dblTest);
int sum = (int)(floatTest+dblTest);
b. System.out.println(“Sum = “+sum);
c. boolean boolTest = true;
float floatTest = 3.14f;
d. System.out.println(“floatTest = “+floatTest);
dblTest = (double)(boolTest ? 1:0);
e. double dblTest = 0.0000000053;
floatTest = (float)(boolTest? 1:0);
Decision-making and Iterations
1. You are trying to use the if statement to display the value “100 Not Equal”. Which of the follow code will help you achieve this.
a. int value = 100;
boolean bool = true;
if((bool==true)&&((value+=100)==200)){
System.out.printf(“Equal “+value);
}else{
System.out.println(value+” Not Equal”);
}
b. int value = 100;
boolean bool = false;
if((bool==true)&&((value==100)==200)){
System.out.printf(“Equal “+value);
}else{
System.out.println(value+” Not Equal”);
}
c. int value = 100;
boolean bool = true;
if((bool==false)&&((value+=100)==200)){
System.out.printf(“Equal “+value);
}else{
System.out.println(value+” Not Equal”);
}
d. int value = 100;
boolean bool = true;
if((bool==true)&&((value+=100)==200)){
System.out.printf(“Equal “+value);
}else{
System.out.println(value+” Not Equal”);
}
2. You are using switch – case statement to display the output “value is ten” and “value is twenty”. Which of the following code will help you achieve this
a. int value = 10;
switch(value){
default:
System.out.println(“Invalid value”);
break;
case 10:
System.out.println(“value is ten”):
break;
case 20:
System.out.println(“value is twenty”);
break;
}
b. int value = 10;
switch(value){
default:
System.out.println(“Invalid value”);
break;
case 10:
System.out.println(“value is ten”):
break;
case 20:
System.out.println(“value is twenty”);
}
c. int value = 10;
switch(value){
default:
System.out.println(“Invalid value”);
break;
case 10:
System.out.println(“value is ten”):
case 20:
System.out.println(“value is twenty”);
break;
}
d. int value = 10;
switch(value){
default:
System.out.println(“Invalid value”);
break;
case 10:
System.out.println(“value is ten”):
case 20:
System.out.println(“value is twenty”);
}
3. You want the output to be display as “95 91 87 83”. Can you arrange the steps in the sequence to achieve the same
- System.out .println(i);
- i++;}
- int i=100
- i-=5;
- while (i>=85){
4. do – while test the condition after the first iteration of the loop
- True đ úng
- False
5. In do – while, if the condition is true, the control passes back to the top of the loop
- True đung
- False
6. In do-while if the condition is true, the loop terminates
- True
- False đ úng
7. do – while statements is executed at least one
- True đ úng
- False
8. do – while tests the condition at the beginning of the loop
- True
- False đ úng
9. You want to display the output as “15105”. Can you indentify the correct code
a. for (int i=15; i>=0; i-=5)
System.out.print(i);
System.out.println(“”);
b. for (int i=11; i<=15; i+=5)
System.out.print(i);
System.out.println(“”);
c. for (int i=15; i>0; i-=5) đ úng
System.out.print(i);
System.out.println(“”);
d. for (int i=15; i>=0; i-=6)
System.out.print(i);
System.out.println(“”);
10. Executed at least once before the condition is checked at the end of the loop
- while
- do – while đ úng
- for
11. Used when the user is sure about the number of the iterations required
- while
- do – while
- for đug
12. Variable used in the expression initialized before the loop starts
- while
- do – while
- for đ úng
13. This control structure is known as post-test loop
- while
- do – while
- for
14. You are using a code snippet to print the value of sum as “6”. Which of the following code snippet will help you to achieve this
a. int sum =0;
int number = 1;
do{
number++;
sum +=number;
if (sum>4) break;
} while ( number<5);
System.out.println(sum);
b. int sum =0;
int number = 0;
do{
number++;
sum +=number;
if (sum>6) break;
} while ( number<5);
System.out.println(sum);
c. int sum =0;
int number = 0;
do{
number++;
sum +=number;
if (sum>4) break;
} while ( number<5);
System.out.println(sum);
d. int sum =0;
int number = 0;
do{
number++;
sum +=number;
if (sum>4) break;
} while ( number==5);
System.out.println(sum);
Introducing classes
1. Class names cannot be a keyword in java
- True dung
- False
2. Class names can be in mixed case
- True đung
- False
3. Declaration of the class need not be preceded with the keyword class
- True
- False đ úng
4. Class names can be begin with a digit
- True
- False đ úng
5. Class names can be begin with a dollar symbol or an underscore character
- True đ úng
- False
6. Constructor is executed when a class is created
- True đ úng
- False
7. Constructors does not have parameters but have return types
- True
- False đ úng
8. The new keyword creates an object of the class
- True đ úng
- False
9. Constructors have the same name as that of the class
- True đ úng
- False
10. A default no – argument constructor has to be defined for every class
- True đ úng
- False
11. You want to declare class Student containing an instance variable rollNo. Further you want to create two instances of this class namely objJohn and objMartin, assign value to their respective instance variable and display them. Can you arrange the steps in sequence to achieve the same
a. objJohn.rollNo = 1151;
Student objMartin = new Student();
b. public static void main(String[] arg){
Student objJohn = new Student();
c. System.out.println( objMartin.rollNo);
}}
d. objMartin.rollNo=1152;
System.out.println(objJohn.rollNo);
e. class Student{
int rollNo;
12. You are trying to define an instance method, display for a class named complex. The class contains two instance variables namely real and imaginary. The method, display should accept an object of type complex and display the values of its instance variables. Which of following code will help you achieve this
a. Void display(Complex obj){
System.out.println(real);
System.out.println(imaginary);
}
b. Void display(Complex obj){
System.out.println(obj.real);
System.out.println(obj.imaginary);
}
c. Complex display(Complex obj){
System.out.println(obj.real);
System.out.println(obj.imaginary);
}
d. Void display(){
Complex obj = new Complex();
System.out.println(obj.real);
System.out.println(obj.imaginary);
}
13. Class block initialize is used to initialize complex classes and the initialization code is preceded with static keyword.
- True
- False
14. Class field initializer is used initialize classes variables
- True
- False
15. Class field initialization code is preceded with a static keyword.
- True
- False
16. Object block initializer consists of open and close brace and the initialization code
- True
- False
Arrays
1. You are trying to create an array of ten elements and store the first ten even numbers within it and display the same. Which of the following code will help you achieve this
a. int[] evenNumbers = new int[10];
int number = 2;
for(int count = 0; count<10; count++){
number +=2;
evenNumbers[count]=number;
}
for (count =0; count<10;count++){
System.out.println(evenNumbers[count]);
}
b. int[] evenNumbers = new int[10];
for(int count = 0, number =1; count<10; count++,number++){
number ++;
evenNumbers[count]=number;
}
for (count =0; count<10;count++){
System.out.println(evenNumbers[count]);
}
c. int[] evenNumbers = new int[10];
for(int count = 0, number =2; count<10; count++,number++){
evenNumbers[count]=number;
number = number +2;
}
for (count =0; count<10;count++){
System.out.println(evenNumbers[count]);
}
d. int[] evenNumbers = new int[10];
int number = 0
for(int count = 0, number =1; count<10; count++,number+=2){
evenNumbers[count]=number;
}
for (count =0; count<10;count++){
System.out.println(evenNumbers[count]);
}
2. You are trying to compute the average of temperatures recorded for 50 cities in 365 days using two – dimentional array temps. Average temperature is obtained by adding each day’s temperature and dividing it by 365. Which of following code will help you achieve this.
a. for (int city =0; city<50; city++){
int total;
for(int day =0;day<365; day++{
total = total+temps[city][day];
}
double average = total/365.0;
System.out.println(“Temperature for city “+city +” is “+average);
}
b. for (int city =0; city<50; city++){
int total = 0;
double average = 0;
for(int day =0;day<365; day++{
total = total+temps[city][day];
average = total/day;
}
System.out.println(“Temperature for city “+city +” is “+average);
}
c. for (int city =0; city<50; city++){
int total = 0;
for(int day =0;day<365; day++{
total = total+temps[city][day];
}
double average = total/365.0;
System.out.println(“Temperature for city “+city +” is “+average);
}
d. for (int city =0; city<50; city++){
int total = 0;
double average = 0;
for(int day =0;day<365; day++{
total = total+temps[city][day];
average = total+day;
}
System.out.println(“Temperature for city “+city +” is “+average);
}
3. Use to get the character value at the specified index
- lastIndexOf
- indexOf
- charAt đ úng
4. Use to compare two object and returns an integer
- substring
- compareTo đ úng
5. Can you indentify the correct parameter specifications for the main method
- String[] args đ úng
- String args
- String args[] đ úng
- String arguments
- String arguments()
- String arguments{}
6. Append values to the end of the StringBuilder object
- append
- insert
Package and Access specifiers
1. A package can comprise of classes, interface and packages
- True
- False
2. A class when defined without any package, result in a compile time error
- True
- False
3. The fully qualified name of java class included package name
- True
- False
4. A package does not provide access protection and namespace management
- True
- False
5. While creating packages, the package statement must be the first statement in the source file
- True
- False
6. Allows member of a class to be accessible in all class belonging to that package in which the class is defined
- default
- public
- private
- protect
7. Allows member of a class to be accessible only within the class and within the methods of the class
- public
- private
- protect
8. Allows member of a class to be accessible from any class.
- Public
- Private
- Protect
9. Allows member of a class to be accessible from another class in another package
- Public
- Private
- Protect
10. Which of the following code will generate compile-time error
a. Class First{
int x = 7;
}
class Second{
public static void main(String[] args){
First fst = new First()
System.out.println(fst.x);
}
}
b. class First{
int x = 7;
}
class Second{
public static void main(String[] args){
First fst = new First()
System.out.println(fst.x);
}
}
c. class First{
public int x = 7;
}
class Second{
public static void main(String[] args){
First fst = new First()
System.out.println(fst.x);
}
}
d. class First{
protect int x = 7;
}
class Second{
public static void main(String[] args){
First fst = new First()
System.out.println(fst.x);
}
}
11. final fields cannot be volatile
- True
- False
12. A transient modifier is used to declare fields that are not saved or restored as part of the state of the object
- True
- False
13. Native method are not coded in java
- True
- False
14. Volatile is applied only to constructors, methods, classes and interfaces
- True
- False
15. Native modifier can be apply to constructors, fields, classes and interfaces
- True
- False
Inheritance and interface
1. If a class does not have any super class. Then it is implicitly derived from Object class Which is parent of all java class
- True
- False
2. The process, whereby characteristics and behavior are transmitted from parent to child entity, is called inheritance
- True
- False
3. Inheritance saves time and encourages code reuse and customization
- True
- False
4. A class can be derived from several classes
- True
- False
5. Super keyword allows the method of super class to be invoked from subclass method
- True
- False
6. super() invoked default constructor of super class
- True
- False
7. super() can be place anywhere in the constructor of the subclass
- True
- False
8. The overriding method must have the same signature as the overridden method
- True
- False
9. The access specifier of the overriding method and the overridden method have to public
- True
- False
10. Indentify the code that overloads method Draw();
a. public class Shape{
public void Draw(){
System.out.println(“drawing shape”);
}
public void Draw(float height){
System.out.println(“drawing shape”);
}
public void Draw(float width){
System.out.println(“drawing shape”);
}
}
b. public class Shape{
public void Draw(){
System.out.println(“drawing shape”);
}
public void Draw(float height){
System.out.println(“drawing shape”);
}
public void Draw(int width){
System.out.println(“drawing shape”);
}
}
c. public class Shape{
public void Draw(){
System.out.println(“drawing shape”);
}
public void Draw(int height){
System.out.println(“drawing shape”);
}
public void Draw(int width){
System.out.println(“drawing shape”);
}
}
d. public class Shape{
public void Draw(){
System.out.println(“drawing shape”);
}
public void overload Draw(float height){
System.out.println(“drawing shape”);
}
public void overload Draw(int width){
System.out.println(“drawing shape”);
}
}
11. You want to declare an abstract class Shape having abstract method area(). Further you provide an implementation of abstract method area() in a subclass Square. The main method creates an instance of Square class and invokes it area() method. Can you arrange the steps in sequence to achieve the same
a. public static void main(String[] args){
Square obj = new Square();obj.side = 10;
b. class Square extends Shape{
int side;
c. void area(){
System.out.println(“Area = “ +side*side);}
d. abstract class Shape{
abstract void area();}
e. obj.area(); }
}
12. You are trying to declare a class JavaProgramer such that its method setExperence() cannot be overridden and its integer constant MIN_EXPERIENCE is set to two. Which of the following code will help you achieve the same
a. class JavaProgramer{
final MIN_EXPERIENCE = 2;
abstract void setExperence(int exp){
this.experience = exp;
}
}
b. class JavaProgramer{
final int MIN_EXPERIENCE = 2;
final void setExperence(int exp){
this.experience = exp;
}
}
c. final class JavaProgramer{
final MIN_EXPERIENCE = 2;
void setExperence(int exp){
this.experience = exp;
}
}
d. class JavaProgramer{
final float MIN_EXPERIENCE = 2;
void final setExperence(int exp){
this.experience = exp;
}
}
13. Given an interface Transaction containing following method :
void deposit(int amount); void withdraw(int amount);
indentify which of the following codes correctly implement Transaction interface
a. class BankAccount implements Transaction{
void deposit(double amount){
//code to deposit amount
}
void withdraw(double amount){
//code to withdraw amount
}
}
b. class BankAccount implements Transaction{
void deposit(int amount){
//code to deposit amount
}
void withdraw(int amount){
//code to withdraw amount
}
}
c. class BankAccount implements Transaction{
public void deposit(int amount){
//code to deposit amount
}
public void withdraw(int amount){
//code to withdraw amount
}
}
d. class BankAccount implements Transaction{
void deposit(int amount){
//code to deposit amount
}
void withdraw(float amount){
//code to withdraw amount
}
}
More on classes
1. static initialization block is preceded by the keyword static
- True
- False
2. static keyword in a class variable implies that instances of the class cannot share the same
- True
- False
3. Static method can access only classes variables and methods directly
- True
- False
4. Static initialization block can reference only this class variables that have declared before it
- True
- False
5. Static initialize can be place only in the main class
- True
- False
6. A nested class can access private member of outer class
- True
- False
7. An anonymous class does not allow the use of extends keyword
- True
- False
8. A static nested class cannot access class methods and variables directly
- True
- False
9. Public static nested classes are used to group classes
- True
- False
10. Modifiers can be used in local classes
- True
- False