Java test answer – Upwork.

Content Protection by DMCA.com

Question 1: What is the output of the given program?

public class Kieblog {
    public static void main(String[] args) {
         String s = "abcd";
         System.out.println(s.substring(2));
    }
}
  1. b
  2. bcd
  3. c
  4. cd

Question 2: Which sequence will be printed when the following program is run?

import java.util.*;
public class KieBlog {
    public static void main(String str[]) {
      List l = new ArrayList();
      l.add("1");
      l.add("2");
      l.add(1, "3");
      System.out.println(l);
    }
}
  1. [1, 3, 2]
  2. [1, 3, 1]
  3. [1, 1, 3]
  4. [1, 1, 2]
  5. This code will generate an error.

Answer: Đáp án A, lưu ý add vào vị trí thứ 1, giá trị là 3 (array index bắt đầu từ 0).

Question 3: What will be written to the standard output when the following program is run?

public class KieBlog {
  public static void main(String args[]) {
  	System.out.println(11 ^ 2);
  }
}
  1. 10
  2. 9
  3. 11
  4. 13
  5. 121

Answer: Đáp án B, ^ là biểu thức XOR. 11 là 1011, 2 là 10. Suy ra 10 XOR 1011 = 1001 (9)

Question 4: What is the output of the given console application?

public class KieBlog {
public static void main(String[] args) {
test();
}
public static void test() {
  try {
  		System.out.print(“-try”);
  		return;
  } catch (Exception e) {
  		System.out.print(“-catch”);
  } finally {
  		System.out.print(“-finally”);
  }
  }
}
  1. –try
  2. -try-catch
  3. -try-finally
  4. -try-catch-finally

Answer: Đáp án C, đắn đo ở chỗ return, nhưng finally luôn được execute trong bất kỳ trường hợp nào.

Question 5: What is/are the OOP concepts demonstrated by this code:

public class Kieblog {
  public static void main(String[] args) {
    Animal a = new Dog();
    new Hospital().treatAnimal(a);
  }
}
class Animal {
  public void sayIt(){
  }
}
class Dog extends Animal{
  public void sayIt(){
  	System.out.println(“I am Dog”);
  }
}
class Cat extends Animal{
  public void sayIt(){
  	System.out.println(“I am Cat”);
  }
}
class Hospital{
  public void treatAnimal(Animal a) {
    if (a instanceof Dog) {
      a.sayIt();
    } else {
      a.sayIt();
    }
  }
}
  1. Polymorphism
  2. Inheritence
  3. Abstraction

Answer: Đáp án A,B. Cả Dog và Cat đều kế thừa từ Animal. Còn đa hình ở chỗ class Animal có khi là Dog, khi là Cat tùy thuộc vào instance của a.

Question 6: What will be the output of this program?

public class KieBlog {
  public static void main (String args[]) {
    String a, b, c, d;
    a = “Hello1234”;
    b = “Hello” + String.valueOf(1234);
    c = “Hello” + “1234”;
    d = new String (new char[]{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘1’, ‘2’, ‘3’, ‘4’});
    System.out.print (a == b);
    System.out.print (“ “);
    System.out.print (a.equals(b));
    System.out.print (“ “);
    System.out.print (a == c);
    System.out.print (“ “);
    System.out.print (a.equals(c));
    System.out.print (“ “);
    System.out.print (a == d);
    System.out.print (“ “);
    System.out.print (a.equals(d));
    System.out.print (“ “);
  }
}
  1. true true true true false true
  2. false true true true false false
  3. false true true true false true
  4. false false true true false true

Answer: Đáp án C. Lưu ý rằng operator “==” chỉ so sánh vùng tham chiếu (reference) trên heap, chứ không so sánh giá trị của string. Phương thức được ưu tiên luôn là equal. Nên a == c là true, nhưng a ==d thì false. Có thể tham khảo hình dưới để hiểu thêm.

Question 7: What is the output of the given console application?

public class KieBlog {
  public static void main(String[] args) {
    try {
      	System.out.print(“-try”);
      	int[] a = {1, 2};
      	a[2] = 0;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
    	System.out.print(“-catch”);
    } catch (Exception e) {
    	System.out.print(“-clean”);
    } finally {
    	System.out.print(“-finally”);
    }
  }
}
  1. -try-finally
  2. -try-clean-finally
  3. -try-catch-finally
  4. -try-catch-clean-finally

Answer: Đáp án C. Mảng a chỉ có 2 phần tử, start index default là 0 -> a[2] sẽ throw Exception (ArrayIndexOutOfBoundsException) -> catch -> không catch exception “clean”

Question 8: What is the output of the following program?

public class KieBlog {
  static String str = “Hello World”;
  public static void changeIt (String s) {
  	s = “Good bye world”;
  }
  public static void main (String[] args) {
    changeIt (str);
    System.out.println(str);
  }
}
  1. Compilation error
  2. Hello World
  3. Good bye world

Answer: Function changeIt không hề tác động tới giá tị str -> Đáp án B

Question 9:  Which of the following statements will not compile?

  1. File f = new File(“/”,”autoexec.bat”);
  2. DataInputStream d = new DataInputStream(System.in);
  3. RandomAccessFile r = new RandomAccessFile(“OutFile”);
  4. OutputStreamWriter o = new OutputStreamWriter(System.out);

Answer: Đáp án C. RandomAccessFile required two parameter.

Question 10: What is the output of the given program?

public class KieBlog {
  public static void main(String[] args) {
    String s = “string102”;
    String t = “string” + (9 * s.length() + 3);
    String u = “string” + 102;
    System.out.println( (s==t) + “-” + (s==u) );
  }
}

  1. false-false
  2. false-true
  3. true-false
  4. true-true

Answer: Đáp án B. Lưu ý nhỏ, khi thực hiện “string” + 102 thì java tự động convert 102 sang kiểu String -> “string102”

Question 11: Which of the following are “keywords” in Java?
Note: There may be more than one right answer.

  1. Default
  2. NULL
  3. String
  4. Throws

Answer: Đáp án A và D. Câu hỏi này khá hay vì NULL ở đây chỉ được xem như là literial, gần giống như True hay False. Còn String, “The Java language specification allows for representation of a string as a literal.” Ngôn ngữ Java đặc tả String như là literal -> Loại C.

Question 12: Which of the following are the methods of the Thread class?.

  1. stay()
  2. go()
  3. yield()
  4. sleep(long millis)

Answer: Đáp án C và D. Dành cho bạn nào chưa biết về yeild(). “yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should run. Otherwise, the current thread will continue to run.” – yield về cơ bản là khai báo ưu tiên cho thread hoặc process. Tham khảo.

Question 13: What is the result of compiling and running the given code?.

public class KieBlog {
    public static void main (String[] args) {
    System.out.println(new A() {{}}.toString());
    }
}
class A {
    public String toString() { 
      return getClass().getName(); 
   }
}
  1. It gets a compiler error.
  2. It compiles, but throws NullPointerException at run-time.
  3. It compiles, runs, and prints “KieBlog” (without quotation marks).
  4. It compiles, runs, and prints “KieBlog$1” (without quotation marks).

Answer: Câu hỏi này có tí xíu mẹo. Khi một lớp khai báo dưới một lớp khác (A dưới KieBlog) dấu $ được sử dụng để làm ký tự phân tách cho trình biên dịch Java. Một class ẩn danh (anonymous class) như là A sẽ là “KieBlog$1.class” -> getName = KieBlog$1

Question 14: In generics code, the question mark (?) is called the?

  1. Wildcard
  2. Raw type
  3. Diamond
  4. Type parameter

Answer: Đáp án A. Question Mark (dấu hỏi), còn được gọi là Wildcard (The question mark (?) is known as the wildcard in generic programming).

Question 15:  What is the output of the given code?

public class KieBlog {
  public static void main(String[] args) {
    VO a = new VO(2);
    VO b = new VO(3);
    swapONE(a, b);
    print(a, b);
    swapTWO(a, b);
    print(a, b);
  }
  private static void print(VO a, VO b) {
      System.out.print(a.toString() + b.toString());
  }
  public static void swapONE(VO a, VO b) {
    VO tmp = a;
    a = b;
    b = tmp;
  }
  public static void swapTWO(VO a, VO b) {
    int tmp = a.x;
    a.x = b.x;
    b.x = tmp;
  }
}
class VO {
  public int x;
  public VO(int x) {
    this.x = x;
  }
  public String toString() {
    return String.valueOf(x);
  }
}
  1. 2332
  2. 3232
  3. 3223
  4. 2323

Answer: Đáp án A. SwapOne không hoán đổi gia trí x, không tham chiếu trực tiếp đến biến này ở class VO.

Question 16:  Which of the following JDBC methods is used to retrieve large binary objects?

  1. getBinaryStream()
  2. getText()
  3. getAsciiStream
  4. getString()
  5. getUnitStream()

Answer: Đáp án A.

Question 17:  Which of the following JDBC methods is used to retrieve large binary objects?

  1. getBinaryStream()
  2. getText()
  3. getAsciiStream
  4. getString()
  5. getUnitStream()

Answer: Đáp án A.

Question 18:  What will be the output of the following program?

public class KieBlog {
  public static void main (String args[]) {
    B o = new A ();
    System.out.println (o.content ());
  }
  public String content () throws Exception {
      throw new Exception (“This is an exception on this.content ()”);
  }
  private static class B {
    public String content () {
      return “B”;
    }
  }
  private static class A extends B {
    public String content () {
      return “A”;
    }
  }
}
  1. The code will compile but will fail to run.
  2. The code will compile and on running, it will print “A”
  3. The code will fail to compile
  4. The code will compile and on running, it will print “B”

Answer: Đáp án B.

Question 19:  Which of these is not an event listener adapter defined in the java.awt.event package?

  1. ActionAdapter
  2. MouseListener
  3. WindowAdapter
  4. FocusListener

Answer: Đáp án A. java.awt.event không có ActionAdapter.

Question 20:  Which of the following is the name of the cookie used by Servlet Containers to maintain session information?

  1. SESSIONID
  2. SERVLETID
  3. JSESSIONID
  4. CONTAINERID

Answer: Đáp án C. Để maintain session information Servlet Containers sẽ sử dụng JSESSIONID.

Question 21:  Which of these interfaces is the most applicable when creating a class that associates a set of keys with a set of values?

  1. Collection
  2. Set
  3. Map
  4. SortedSet

Answer: Đáp án C. Một tập hợp các keys và values.

Có gì thắc mắc cứ comment đây nha! - Please feel free to comment here!

Kiên Nguyễn

👋 HEY THERE! 👋. My pleasure when you're here to read the article. Please feel free to comment, send a message or just want to say HELLO. Thank you, from bottom of my heart ❤️❤️❤️. Visit my portfolio: https://kieblog.com

Mời tui ly cà phê

Like page để không bỏ lỡ bài viết mới
Facebook Pagelike Widget
Bài viết mới
Lưu trữ