Mock Exam 3 Answers
Mock Exam 3 Answers
Mock Exam 3 Answers
Select 1 option(s):
A. new, delete, and goto are keywords in the Java language delete & exit is method, throws
B. try, catch, and thrown are keywords in the Java language not thrown, unsigned is wrong
C. static, unsigned, and long are keywords in the Java language
D. exit, class, and while are keywords in the Java language
E. return, goto, and default are keywords in the Java language
2. What can be inserted in the following code so that it will print true when run?
System.out.println(flag);
Select 1 option(s):
A. boolean flag = s1.contains("bella"); indexOf() to check index of " "
contains() check is the list contains " "
B. boolean flag = s1.indexOf("bella")>1; length() for array, list uses size()
C. boolean flag = s1.contains("bella") == 1;
D. boolean flag = s1.length()>1;
5. What will the following code print when compiled and run?
}
Select 1 option(s):
A. It will not compile.
B. It will throw an exception at runtime.
C. It will print 0.
D. It will print 5.
E. None of these.
8. What will the following lines of code print?
String s = "java";
s.replace('j', 'l'); replace but tak set kat s that's why dia ambik yg java, s = s.replace('j', 'l') baru tukar lava
s = s.substring(0, 2);
System.out.println(s);
Select 1 option(s):
A. java
B. lava
C. la
D. ja
E. lav
class Test
{
public static void main(String[] args)
{
int j = 1; doIt() throw exception so dia catch and print la "j = " + j, j is initialize
try as 1 so it'll print j = 1;
If ada finally, finally will be printed gak
{ 1. buat doIt()
int i = doIt() / (j = 2);
} catch (Exception e)
{ 3. catch exception, so buat la yg syso j =
System.out.println(" j = " + j);
}
} 2. throw exception
public static int doIt() throws Exception { throw new
Exception("FORGET IT"); }
}
Select 1 option(s):
A. It will print j = 1;
B. It will print j = 2;
C. The value of j cannot be determined.
D. It will not compile.
E. None of the above.
class TestClass
{
static TestClass ref; Create object class
String[] arguments;
public static void main(String args[])
{
ref = new TestClass();
ref.func(args); syso ref akan print class name, package and memory
}
public void func(String[] args)
{
ref.arguments = args;
}
}
Select 1 option(s):
A. The program will fail to compile, since the static method main is trying to call the
non-static method func.
B. The program will fail to compile, since the non-static method func cannot access the
static member variable ref.
C. The program will fail to compile, since the argument args passed to the static
method main cannot be passed on to the non-static method func.
D. The program will fail to compile, since method func is trying to assign to the non-
static member variable 'arguments' through the static member variable ref.
E. The program will compile and run successfully.
21. What will the following method return if called with an argument of 7?
}
Select 1 option(s):
A. 7
B. 14 else 2 kali
C. 49
D. Compilation fails.
22. How many times will the line marked //1 be called in the following code?
int x = 10;
do
{
x--;
System.out.println(x); // 1
} while(x<10);
Select 1 option(s):
A. 0
B. 1 x-- will always be < 10 so while will be always true, so line will always be called
C. 9
D. 10
E. None of these.
23. Which of the following implementations of a max() method will correctly return the
largest value?
Select 1 option(s):
A. int max(int x, int y) A and B return statement invalid syntax
C sbb ada condition dlm switch
{
return( if(x > y){ x; } else{ y; } );
}
B. int max(int x, int y)
{
return( if(x > y){ return x; } else{ return y; } );
}
C. int max(int x, int y)
{
switch(x < y) cannot put condition in switch, letak variable je nanti dia tengok
{ variable tu pass apa
case true:
return y;
default :
return x;
};
}
D. int max(int x, int y)
{
if (x > y) return x;
return y;
}
24. What will the following program print?
27. Using a break in a while loop causes the loop to break the current iteration and start
the next iteration of the loop.
Select 1 option(s):
A. True
B. False break will stop the whole loop, not the current iteration only
28. Which of the following statements will correctly create and initialize an array of Strings
to non null elements?
Select 4 option(s):
A. String[] sA = new String[1] { "aaa"}; cannot assign size terus
B. String[] sA = new String[] { "aaa"};
C. String[] sA = new String[1] ; sA[0] = "aaa";
D. String[] sA = {new String( "aaa")};
E. String[] sA = { "aaa"};
29. What will be the output of the following program?
public static void amethod(){ } if method kosong pun boleh je as long as dia void
}
Select 1 option(s):
A. try finally
B. try finally out
C. try out
D. catch finally out
E. It will not compile because amethod() does not throw any exception.
30. Which of the following code snippets will print exactly 10?
1.
Object t = new Integer(106);
int k = ((Integer) t).intValue()/10;
System.out.println(k);
2. System.out.println(100/9.9);
2 and 3 will print in decimal point
3. System.out.println(100/10.0);
4. System.out.println(100/10);
5. System.out.println(3 + 100/10*2-13);
Select 3 option(s):
A. 1
B. 2
C. 3
D. 4
E. 5
31. What is the result of compiling and running the following program?
String str = "asdfasdf"; remove() will remove the first alphabet found, but replace() will replace
char ch = str.charAt(3); all bcs remove() returns boolean and replace() returns string
if(ch == 'a') str = str.replace('a', 'x');
else if(ch == 'f') str = str.replace('s', 'x');
System.out.println(str);
Select 1 option(s):
A. asdfasdf
B. axdfaxdf
C. axdfasdf
D. xsdfxsdf
E. xsdfasdf
34. What will the following code print when compiled and run?
import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
List list = new ArrayList();
list.add("val1"); //1 arraylist elements need to be added in turn following the
list.add(2, "val2"); //2 next index
list.add(1, "val3"); //3 val1 is added into index 0, so val2 can only be added
System.out.println(list); into index 1
}
}
Select 1 option(s):
A. It will not compile.
B. It will throw an exception at run time because of line //1
C. It will throw an exception at run time because of line //2
D. It will throw an exception at run time because of line //3
E. Null
package x;
public class TestClass {
ArrayList<String> al;
public void init(){
al = new ArrayList<>();
al.add("Name 1");
al.add("Name 2");
}
public static void main(String[] args) throws Exception {
TestClass tc = new TestClass();
tc.init();
System.out.println("Size = "+tc.al.size());
}
}
Which import statement should be added to make it compile?
Select 1 option(s):
A. import java.lang.*;
B. import java.lang.ArrayList;
C. import java.util.ArrayList;
D. import java.collections.ArrayList;
E. No import is necessary.
38. Which of the following expressions will evaluate to true if preceded by the following
code?
String a = "java";
char[] b = { 'j', 'a', 'v', 'a' };
String c = new String(b);
String d = a;
Select 3 option(s):
A. (a == d)
B. (b == d)
C. (a == "java") same data type boleh guna ==
D. a.equals(c) diff data type kena guna equals()
39. Given that java.lang.Integer class has a public static field named MAX_VALUE,
which of the given options should be inserted at line 1 so that the following code can
compile without any errors?
package objective1;
// 1
public class StaticImports
{
public StaticImports()
{
out.println(MAX_VALUE);
}
Select 2 option(s):
A. import static java.lang.Integer.*;
B. static import java.lang.System.out;
C. static import Integer.MAX_VALUE;
D. import static java.lang.System.*;
E. static import java.lang.System.*;
40. Which of the following are true about the "default" constructor?
Select 2 option(s):
A. It is provided by the compiler only if the class does not define any constructor.
B. It initializes the instance members of the class.
C. It calls the default 'no-args' constructor of the super class.
D. It initializes instance as well as class fields of the class.
E. It is provided by the compiler if the class does not define a 'no- args' constructor.
void crazyLoop()
{
int c = 0;
JACK: while (c < 8)
{
JILL: System.out.println(c);
if (c > 3) break JILL; else c++;
}
}
Select 1 option(s):
A. It will not compile. break is applicable to break loop only, so need to break at JACK
B. It'll throw an exception at runtime.
C. It will print numbers from 0 to 8
D. It will print numbers from 0 to 3
E. It will print numbers from 0 to 4
class Test
{
public static void main(String args[])
{
int var = 20, i=0;
do
{
while(true)
{
if( i++ > var) break; loop sampai i jadi 21 then break bcs 21 > 20 is true
}
}while(i<var--); 21 < 20--
System.out.println(var); 19
}
}
Select 1 option(s):
A. 19
B. 20
C. 21
D. 22
E. It'll enter an infinite loop.
51. You have been given a library that contains the following class:
package com.cool;
public class Cooler {
public void doCool(){
System.out.println("cooling...");
}
}
Now, you are writing another class that makes use of the above library as follows:
52. What will be printed when the following code snippet is executed?
53. What will the following code print when run without any arguments ...
public class TestClass {
54. Given:
class Account{
//insert code here
}
What can be inserted in the above code at the specified location without causing
compilation error?
Select 1 option(s):
A. {
private String id; field declaration doesn't need curly braces
}
B. while(true) {
System.out.println("true"); takde main method
}
C. package org.acme; cannot put package in class
D. private String id = "hello";
void print(){
System.out.println(id);
} still error bcs takde main, but its runtime error not compile error
55. Given the code fragment:
while(j>0)
{
System.out.println(j--);
if(j == 4) break POINT1;
}
}
What will it print?
Select 1 option(s):
A. It will print 1 and 2
B. It will print 1 to N where N is a random number.
C. It will not compile. while is a diff block of code, it cannot access POINT1
D. It will throw an exception at runtime.
57. Given:
String s1 = "Hello";
String s2 = "World";
//1
Which of the following options are valid when inserted independently at the line
marked //1?
Select 3 option(s):
A. s1 += s2;
B. s1 -= s2;
C. System.out.println( s1 = s2 );
D. System.out.println( s1 == s2);
58. What will be the result of attempting to compile the following program?
public class TestClass
{ this is a method not constructor bcs
long l1; there's void
public void TestClass(long pLong) { l1 = pLong ; } //1
public static void main(String args[])
{
TestClass a, b ;
a = new TestClass(); //2
b = new TestClass(5); //3 no constructor created for this class, so 5 cannot
} be passed as parameter
}
Select 1 option(s):
A. A compilation error will be encountered at //1, since constructors should not specify
a return value.
B. A compilation error will be encountered at //2, since the class does not have a
default constructor.
C. A compilation error will be encountered at //3.
D. The program will compile correctly.
E. It will not compile because parameter type of the constructor is different than the
type of value passed to it.