Student_Test_Java_CORE
Student_Test_Java_CORE
NAME:
EMAIL:
DATE:
1
Question # 1
Which of the following are keywords or reserved words in Java?
A. if B. then
C. goto D. while
E. case
Question # 2
A byte can be of what size
A. -128 to 127
B. (-2 power 8)-1 to 2 power 8
C. -255 to 256
D. depends on the particular implementation of the Java Virtual machine
Giải thích: Kiểu dữ liệu byte trong Java sử dụng 8-bit, nên phạm vi giá trị là từ -2^7 đến 2^7 - 1, tương đương -128 đến
127.
Question # 3
What will happen if you try to compile and run the following code?
public class Q {
public static void main(String argv[]){
int anar[]=new int[]{1, 2, 3};
System.out.println(anar[1]);
}
}
A. 1 B. Error anar is referenced before it is initialized
C. 2 D. Error: size of array must be defined
Question # 4
Given the following declarations
String s1=new String("Hello");
String s2=new String("there");
String s3=new String();
Which of the following are legal operations?
2
3
Question # 5
What will happen when you compile and run the following code?
public class MyClass{
static int i;
public static void main(String argv[]){
System.out.println(i);
}
}
4
Question # 8
What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current
directory?
import java.io.*;
public class Mine {
public static void main(String argv[]){
Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod() {
try {
FileInputStream dis=new FileInputStream("Hello.txt");
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
return -1;
}catch(IOException ioe) {
} finally{
System.out.println("Doing finally");
}
return 0;
}
}
A. No such file found
B. No such file found, -1
C. No such file found, Doing finally, -1
D. 0
Giải thích: Khi file không tồn tại, catch (FileNotFoundException) in ra thông báo, finally luôn được thực thi, và phương
thức trả về -1.
Question # 9
Which of the following statements are true?
A. System.out.println(-1>>>2); will output a result larger than 10
B. System.out.println(-1>>>2); will output a positive number
C. System.out.println(2>>1); will output the number 1
D. System.out.println(1<<<2); will output the number 4
Giải thích: Toán tử dịch phải không dấu (>>>) chuyển đổi các bit sang dương.
5
Question # 10
What will be displayed when you attempt to compile and run the following code
//Code start
import java.awt.*;
public class Butt extends Frame{
public static void main(String argv[]){
Butt MyBut=new Butt();
}
Butt(){
Button HelloBut=new Button("Hello");
Button ByeBut=new Button("Bye");
add(HelloBut);
add(ByeBut);
setSize(300,300);
setVisible(true);
}
}
//Code end
A. Two buttons side by side occupying all of the frame, Hello on the left and Bye on the right
B. One button occupying the entire frame saying Hello
C. One button occupying the entire frame saying Bye
D. Two buttons at the top of the frame one saying Hello the other saying Bye
Giải thích: FlowLayout (mặc định) căn các nút từ trái qua phải, trên cùng của khung.
Question # 12 - 11
If g is a graphics instance what will the following code draw on the screen?
g.fillArc(45,90,50,50,90,180);
A. An arc bounded by a box of height 45, width 90 with a centre point of 50, 50, starting at an angle of 90 degrees
traversing through 180 degrees counter clockwise.
B. An arc bounded by a box of height 50, width 50, with a centre point of 45, 90, starting at an angle of 90 degrees
traversing through 180 degrees clockwise.
C. An arc bounded by a box of height 50, width 50 with a top left at coordinates of 45, 90, starting at 90 degrees and
traversing through 180 degrees counter clockwise.
D. An arc starting at 45 degrees, traversing through 90 degrees clockwise bounded by a box of height 50, width 50
with a centre point of 90, 180.
Giải thích: Hàm fillArc(x, y, width, height, startAngle, arcAngle) vẽ cung trong hình chữ nhật.
6
7
Question # 11 - 12
If you wanted to find out where the position of the letter v (ie return 2) in the string s containing "Java", which of the
following could you use?
A. mid(2,s); B. charAt(2);
C. s.indexOf('v'); D. indexOf(s,'v');
Giải thích: Phương thức indexOf(char) tìm vị trí đầu tiên của ký tự trong chuỗi.
Question # 13
What code placed after the comment //For loop would populate the elements of the array ia[] with values of the variable i.?
public class Lin{
public static void main(String argv[]){
Lin l = new Lin();
l.amethod();
}
public void amethod(){
int ia[] = new int[4];
//Start For loop
{
ia[i]=i;
System.out.println(ia[i]);
}
}
}
Question # 15 - 14
An Applet has its Layout Manager set to the default of FlowLayout. What code would be correct to change to another
Layout manager?
A. set LayoutManager(new GridLayout());
B. set Layout(new GridLayout(2,2));
C. setGridLayout(2,2);
D. setBorderLayout();
Giải thích: Phương thức setLayout() dùng để thay đổi Layout Manager.
8
Question # 14 - 15
Which of the following will output -4.0
A. System.out.println(Math.floor(-4.7)); B. System.out.println(Math.round(-4.7));
C. System.out.println(Math.ceil(-4.7)); D. System.out.println(Math.min(-4.7));
Giải thích: Math.floor() làm tròn xuống, kết quả là -4.0.
Question # 16
What will be the result when you attempt to compile and run the following code?
public class Conv{
public static void main(String argv[]){
Conv c=new Conv();
String s=new String("ello");
c.amethod(s);
}
public void amethod(String s){
char c='H';
c+=s;
System.out.println(c);
}
}
A. Compilation and output the string "Hello"
B. Compilation and output the String "ello"
C. Compilation and output the string elloH
D. Compile time error
Giải thích: Biến char không thể cộng trực tiếp với String.
Question # 23 - 17
What will the following code print out?
public class Oct{
public static void main(String argv[]){
Oct o = new Oct();
o.amethod();
}
public void amethod(){
int oi= 012;
System.out.println(oi);
}
}
A. 12 B. 012
C. 10 D. 10.0 Giải thích: 012 là số octal, tương
9
đương 10 trong hệ thập phân.
Question # 17 - 18
What will be printed out if this code is run with the following command line?
java Myprog good morning
public class Myprog{
public static void main(String argv[]){
System.out.println(argv[2])
}
}
A. myprog
B. good
C. morning
D. Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"
Question # 18 - 19
If you create a TextField with a constructor to set it to occupy 5 columns, what difference will it make if you use it with a
proportional font (ie Times Roman) or a fixed pitch typewriter style font (Courier)
A. With a fixed font you will see 5 characters, with a proportional it will depend on the width of the characters
B. With a fixed font you will see 5 characters, with a proportional it will cause the field to expand to fit the text
C. The columns setting does not affect the number of characters displayed
D. Both will show exactly 5 characters
Giải thích: Với font cố định (e.g., Courier), mỗi ký tự có cùng kích thước, nên hiển thị chính xác 5 ký tự. Với font tỷ lệ
(e.g., Times Roman), chiều rộng ký tự khác nhau sẽ ảnh hưởng số ký tự hiển th
Question # 19 - 20
Given the following code:
import java.awt.*;
public class SetF extends Frame{
public static void main(String argv[]){
SetF s=new SetF();
s.setSize(300,200);
s.setVisible(true);
}
}
How could you set the frame su
A. s.setBackground(Color.pink);
B. s.setColor(PINK);
C. s.Background(pink);
D. s.color=Color.pink
10
Question # 20 - 21
Given the following code what will be output?
public class Pass{
static int j=20;
public static void main(String argv[]){
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
public void amethod(int x){
x=x*2;
j=j*2;
}
}
Question # 21 - 22
Which of the following can you perform using the File class?
A. Change the current directory
B. Return the name of the parent directory
C. Delete a file
D. Find if a file contains text or binary information
Question # 24 - 23
What is the result of the following operation?
System.out.println(4 | 3);
A. 6 B. 0
C. 1 D. 7
11
Giải thích:
Question # 22 - 24
What will be the result when you try to compile and run the following code?
private class Base{
Base(){
int i = 100;
System.out.println(i);
}
}
public class Pri extends Base{
static int i = 200;
public static void main(String argv[]){
Pri p = new Pri();
System.out.println(i);
}
}
12
r.i = r.i*2;
}
}
C. Giải thích:
package MyPackage;
importjava.awt.*; Trong Java, từ khóa package phải được
class MyClass{} đặt ở dòng đầu tiên của tệp mã nguồn.
Đoạn mã A không đúng do import xuất
hiện trước package.
Đoạn mã C có lỗi cú pháp (importjava
thiếu khoảng trắng).
Question # 28 - 27
Which of the following will successfully crate an instance of the Vector class and add an element?
A. C.
Vector v=new Vector(99); Vector v=new Vector();
v[1]=99; v.add(99);
B. D.
Vector v=new Vetor(); Vector v=new Vector(100);
v.addElement(99); v.addElement("99");
Giải thích:
13
Vector một cách hợp lệ.
Question # 29 - 28
What will happen when you compile and run the following code?
public class Scope{
private int i;
public static void main(String argv[])}
Scope s = new Scope();
s.amethod();
}//End of main
public static void amethod(){
System.out.println(i);
}//end of amethod
}//End of class
Biến i là instance variable, không thể được truy cập trực tiếp trong phương thức static mà không có instance.
Question # 26 - 29
What will be the result when you attempt to compile this program?
public class Rand{
public static void main(String argv[]){
int iRand;
iRand = Math.random();
System.out.println(iRand);
}
}
A. Compile time error referring to a cast problem
B. A random number between 1 and 10
C. A random number between 0 and 1
D. A compile time error about random being an unrecognised method
Giải thích:
14
Phương thức Math.random() trả về double, không thể gán trực tiếp cho int mà không có phép ép kiểu.
Question # 30
What will happen when you attempt to compile and run the following code
class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver){
System.out.println("Over.amethod");
}
}
Phương thức amethod trong lớp Base là private, nên không thể được truy cập hoặc override bởi lớp Over.
~ The end ~
15