Вы находитесь на странице: 1из 39

Examen Curs 1 (Java Programmer)

1. Witch program must br run when executing a Java class file?

a. the Java compiler

b. the Java Virtual Machine

c. the JVM or equivalent emulator

d. the jar decompression

e. no answer

2. What will be the value of x, y, z after executing the following place of code?

int x = 2;
int y = 2;
int z = (((x++) + (++y)) > 5 && ((x++)+y) < 10) ? 1 : 0;

a. x: 3, y: 4, z: 1

b. x: 3, y: 3, z: 0

c. x: 4, y: 4, z: 1

d. x: 5, y: 5, z: 1

e. x: 4, y: 3, z: 1

f. x: 2, y: 2, z: 0

g. no answer

3. What is the smallest Java data type that will correctly store the value 135?

a. short

b. boolean

c. float

d. byte

e. long

f. no answer

4. When a public Java class is stored in a Java source code file, what must be true about the
name of the source file relative to the name of the class?

a. names must be the same, but case is irrelevant

b. names must start with the same letter

c. names must be different

d. names must match exactly, including case

e. no answer

5. Which Java platform is dedicated to portable devices that have limited resources?

a. Java XE

b. Java EE

c. Java ME

d. Java SE

e. no answer

6. How must a String literal be represented in the Java source code?

a. enclosed in “ (double quotes)

b. enclosed in either “ (double quotes) or ‘ (single quotes)

c. enclosed in ` (back quotes)

d. enclosed in ‘ (single quotes)

e. no answer

Page 1 of 39
7. Which tools are required for creating and running a Java program? (choose three)

a. Windows 98 or higher

b. a JVM

c. a compiler

d. an editor

e a DOS interpretor

8. Which of the following are valid char literals? (choose two)

a. ‘E’

b. “\u000E“

c. ‘\u000E’

d. ‘000E’

e. ‘\u0x0045’

9. Given the following variable declaration String s = “Some value”; what is stored in the memory
location referred by the variable s?

a. the character S (the first character in the string)

b. a way to reach the location where the string Some Value is stored

c. the list of characters that comprise the string

d. the character “ (the first character in the String)

e. no answer

10. Which of the following are used to instruct a program to perform mathematical calculation or
to test data?

a. operators

b. punctuation

c. keywords

d. identifiers

e. no answer

11. Which tools does an IDE use that are also used when creating a Java program from the
command line? (choose two)

a. command prompt

b. javac

c. notepad

d. graphical user interface

e. JVM

12. Which of the following interprets byte code to the specific language of the machine?

a. JVM

b. Java compiler

c. bytecode compiler

d. Java source code

e. no answer

13. If a programmer needs the Java compiler, what software must he/she install?

a. an IDE like NetBeans or Eclipse, because the Java compiler is a part of an IDE, not a
separate component

b. JDK

c. nothing; Java is platform-independent, therefore the compiler is included by default in


any operating system

d. JRE

e. no answer

Page 2 of 39
14. Which of the following is NOT a Java primate data type?

a. boolean

b. int

c. String

d. char

e. float

f. no answer

15. Which of the following JDK utility programs is used to compile a Java source code file into
bytecode?

a. jar

b. javadoc

c. java

d. javac

e. no answer

16. What is the suffix of a Java byte code file?

a. .jvm

b. .txt

c. .java

d. .class

e. .cls

f. no answer

17. Which of the following assignments will correctly compile? (choose two)

a. char c = ‘\u0020’;

b. short s = 70000;

c. boolean b = 0;

d. long L = 12L;

e. float f = 1.2;

18. The Java Virtual Machine (JVM) translates and executes code that is stored in which type of
files?

a. .javac files

b. .java files

c. .class files

d. .code files

e. no answer

19. What will happen when a Java programmer has successfully compiled a Java class named 

Test using command line and then issues the following command?

java Test.class

Assume that byte code file resides in the current directory.

a. an error will be displayed, because a class name shouldn’t start with an uppercase letter

b. the class will successfully run

c. an error will be displayed, because the .class extension should not have been specified
as part of the command

d. an error will be displayed, because the file extend sion should be .java instead of .class
when running a Java .class

e. no answer

Page 3 of 39
20. What must a Java class definition contain in order to be able to run?

a. a class name starting with a lowercase letter

b. a start() function

c. at least one expression that involves operators

d. at least one variable definition

e. a main() function with a certain signature

f. no answer

21. Which of the following expressions will produce a value of type String? (choose three)

a. 5+6+7

b. 5+”6"+7

c. 5+6+’7'

d. 5+6+”7"

e. 5+’"'+7

f. 5+(6+”7”)

22. Which of the following are NOT valid Java integer literals in Java version 7 and above?
(choose two)

a. “10001"

b. 0b1001

c.10_01

d. 0x1001

e. -1001

f. 0e1001

23. Which of the following is the machine independent part used in the execution of a Java
program?

a. javac.exe

b. JVM

c. bytecode

d. web browser

e. no answer

24. Which sequence best describes the physical steps taken by the programmer to produce a
running Java program?

a. Write the program c. Write the program

Compile the program Compile to bytecode

Run the program Translate to native code

b. Write the program Run the program

Translate to native code d. Write the program

Compile the byte code Compile to bytecode

Run the program Run the program

e. no answer

25. Which Java data type can store a letter, digit or punctuation mark, but is perceived as numeric
by the compiler and JVM?

a. byte

b. boolean

c. String

d. char

e. no answer

Page 4 of 39
26. A Java class must be able to be executed. Which of the following elements are included in the
signature of the function that must be inserted in the class definition? (choose three)

a. static

b. public

c. final

d. void

e. private

f. int

Examen Curs 2 (Java Programmer)

1. Which of the following is a valid index data type for accessing members of an array?

a. double

b. boolean

c. int

d. String

e. no answers

2. What must be true of the length of an array object once it has been instantiated?

a. the length of an array is variable, depending on the needs of the program

b. the length of an array is a read only final attribute that cannot be change

c. the length of an array is determinate during program execution, after the array object
has been created

d. the length of an array is dependent on the amount of memory available

e. no answer

3. Which statement will crate an array pf type double that will contain the numbers 5.2, 7.0 and
-2.5?

a. double[] list = [5.2, 7.0, -2.5]

b. double list[3] = {5.2, 7.0, -2.5}

c. double[] list = array(5.2, 7.0, -2.5)

d. double[] list = {5.2, 7.0, -2.5}

e. no answer

4. What happens when an array of the objects is created?

a. the array contains objects of the specific type initialised to null or 0

b. the array contains objects of the specific type created earlier in the program

c. the array contains references to objects of the specified type created earlier in the
program

d. the array contains locations for references to objects of the specified type

e. no answer

5. Using the array declaration below, what is the value of t.length ?

int[][] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

a. 4

b. 12

Page 5 of 39
c. 1

d. 3

e no answer

6. Which term refers to storing values in an array?

a. nulling

b. polymorphing

c. posting

d. populating

e. no answer

7. What is combined with an array identifier to provide a reference to a single element of the
array?

a. primitive value

b. index value

c. array value

d. variable value

e. no answer

8. What will Arrays.copyOfRange(t, 1, 3) return if array t has the definition below?

char[] t = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};

a. an array containing a and b

b. an array containing c and d

c. an array containing b and c

d. an array containing b, c, and d

e. no answer

9. Consider the attached code. What is contained in each element of the array when the
statements are executed?

class WhatIsStored
{
public static void main(String s[ ])
{
int numbers[ ];
numbers = new int[2];
}
}

a. 0

b. 2

c. \u000

d. 2.0

e. no answer

10. Which o the following Java flow control structure could be used for repeatedly executing a
block of statements? (choose three)

a. do…while

b. while

c. for

d. if

e. switch

Page 6 of 39
11. Given the array declarations below, which of the following assignments will correctly compiled
and will not produce any runtime errors? (choose three)

int[] a = {3, 1, 7};


int[][] b = {{1, 5}, {6}, {3, 0}};
int[][][] c = {{{1, 2}, {3}}, {{4}}, null, {{5,6}, null}}};

a. c[2][1] = b[2];

b. c[2] = b;

c. c[0][a[1]] = b[c.length/2];

d. b[1] = a[2];

e. b[1] = a;

f. c[0][0] = a[1];

12. How many time will the following loop execute?

int i = 2;
do{
i+=i;
}while ( i < 10 );

a. 2

b. 0

c. 3

d. 4

e. 1

f. no answer

13. How can a Java array named arr that is already full be extended to accommodate a new
element?

a. once created a Java array cannot be extended

b. arr.length++;

c. Arrays.extend(arr, arr.length, arr.length+1);

d. just assign the new value of the variable arr[arr.lengrth+1]

e. no answer

14. What is the index of the last element in an array?

a. the number that is specified as the last element

b. the size of the array minus one

c. the size of the array

d. the index on the number of the bytes used by each element

e. no answer

15. Which of the following values could be used for initialising variable c in order for the following
code to print “I love Java”? (choose two)

int a = 6;
int b = 9;
int c = ?;
if (a-c<0){
if(b-c>0) System.out.println(“I love Java”);
else System.out.println(“I love C++ more”);
} else {
System.out.println(“…but there’s always PHP…”);
}

Page 7 of 39
a. 9

b. 7

c. 10

d. 6.9

e. 8

16. Which of the following are true regarding the sections of the for statement in Java? (choose
three)

for( section1; section2; section3){ }

a. section3 is executed at the beginning of every iteration

b. section1 is executed once

c. section1 is executed at the beginning of every iteration

d. section2 is evaluated at the beginning of every iteration

e. section3 is executed at the end of every iteration

f. section 2 is evaluated at the end of every iteration

17. What is displayed when the program is run?

public class ArrayTest

public static void main( String s[] )

int intArray[] = {0, 4, 2, 5, 3};

char charArray[] = {‘H’, ‘B’, ‘R’, ‘2’};

double dbArray[] = {3.4, 4.5, 2.0, 5.3, 3.1, 8.2, 9.0};

System.out.println(“The output is: “ + dbArray[intArray[2]] +


intArray[charArray[3]]);

a. the exception “ArrayIndexOutOfBoundsException" is thrown

18. What is the element at index 3 in the character array?


‘H' ‘0' ‘A’ ‘5’ ‘3'

a. ‘3’

b. ‘H’

c. ‘A’

d. ‘5’

e. no answer

19. Where using switch(expr) in Java >=7, the data type of expr can be any of the following,
EXCEPT:

a. byte

b. long

c. short

d. String

e. int

f. char

g. no answer

Page 8 of 39
20. What is the output displayed from the program shown?

a. 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,

b. 1.0, 4.0, 9.0, 16.0, 25.0, 36.0,

c. 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,

d. 4.0, 8.0, 12.0, 16.0, 20.0, 24.0,

e. no answer

Examen Curs 3 (Java Programmer)

1. Which of the following are true about the class constructor? (choose three)

a. it cannot be overloaded

b. its name must be exactly match the name of the class

c. it must have a void return type

d. a class always contains a constructor, even if the programmer doesn’t provide one

e. it always has arguments, because it must be used for initialising attributes

f. it is automatically called by virtual machine when instantiating an object

2. Which of the following describes the process of hiding information of an object?

a. encapsulation

b. obscuration

c. construction

d. mutation

e. instantiation

3. Please choose the answer that matches each class member acmes level with its correct
description:

a. the class member is only accessible to the methods of the class

Page 9 of 39
b. the class member is only accessible to classes with the same name as the class that
declares the member

c. the class member is only accessible to classes within the same package as the class
that declares the member

d. access to the class member is unrestricted

1. public 3. private

2. default 4. protected

a. 1-d, 2-c, 3-a, 4-c

b. 1-a, 2-b, 3-c, 4-d

c. 1-d, 2-c, 3-a, 4-b

d. 1-d, 2-c, 3-a, 4-a

e. no answer

4. Which term refers to the data that an object contains?

a. attribute

b. access level

c. modifier

d. method

e. no answer

5. Which term describes a memory resident entity containing both data and instructions the data,
that can be manipulating?

a. object

b. attribute

c. encapsulation

d. class

e. method

f. data type

g. no answer

6. Which of the following best describes the purpose of a mutator method?

a. a mutator is an object method to create and return a new object of the same type

b. a mutator allows the programmer to overload a class behaviour

c. a mutator allows the programmer change the object reference to another object to the
same data type

d. a mutator provides a standard and safe way off changing the value of an object attribute

e. no answer

7. Which of the following are true about the attributes contained in a class definition? (choose
three)

a. when accessing an attribute from outside its class definition, the syntax
reference.attributeName must be used

b. the contractor is the only way to initialise attributes

c. attributes can always be accessed from outside their class definition, given that a
reference to a class object exists

d. attributes can be accessed from inside their class methods using access modifiers

e. class attributes can be made inaccessible to the class methods using access modifiers

f. each object has its own set (copy) of the class attributes

8. Which of the following are benefits of encapsulation? (choose two)

a. encapsulation eliminates the need for getter and setter methods, thus minimising the
number of methods a class must have

Page 10 of 39
b. object data can be kept valid by hiding it in the object and validating any incoming data
using methods

c. the programmer of a class can create multiple methods with the same name in the same
class

d. the programmer a class can change the internal implementation the class as long as its
publicly accessible members remain and act the same

9. Which term describes what objects can do?

a. attribute

b. data type

c. method

d. access modifier

e. qualifier

f. no answer

10. Which of the following is NOT a valid way to call the default constructor of a Java class named
Test?

a. directly, from another constructor of class Test, using this()

b. indirectly, from outside class test, by instantiating an object using new Test()

c. directly, from outside class Test, using the syntax t1.Test(), where t1 is a reference to an
object of type Test

d. indirectly, by instantiating an object inside a method of a class Test, using new Test()

e. no answer

11. Which pairs of method signature show correctly overloaded methods? (choose two)

a. float calcAnswer(int numA, float numB)

float calcResult(int numA, float numB

b. float calcResult(float numA, int numB, int numC)

int calcResult(float numA, int numB)

c. float calcResult(int numA, float numB)

int calcResult(int numB, float numC)

d. float doSomething(int numA)

float doSomething(boolean numA)

12. Which of the following best describes the concept of encapsulation in object-oriented
languages?

a. a garbage collector destroys all objects no longer being referenced and encapsulates
them as they are destroyed

b. the methods to manipulate the data are provided in the class together with data, so the
data is manipulated in a predictable manner

c. data is freely available; the application has no difficulty accessing the data it needs

d. a method can be written several ways in the same class as long as the signature are
different

e. no answer

13. What is true about having the definition below? (choose three)

void f(char…c){
//method body
}

a. the method can be called with 1 ore more arguments of various data types; the first
argument must be of type char

Page 11 of 39
b. this definition is only valid starting with Java 5

c. inside the method, variable c will be an array

d. if no arguments are provided when calling the method, c will have a value of null inside
the method

e. the method can be called with 0 ore more arguments of type char

f. the method can be called with 0 or more arguments of various data types; at least one of
them must be of type char

14. What is required of the value that is returned by a method’s return statement(s)?

a. it must be assignment-compatible with the return type in the method definition

b. it must be one of the arguments passed to the method

c. it must match the argument list

d. it must have the same variable name as the arguments

e. no answer

15. Which of the following are Java keywords that act as access modifiers and can be used fun
the definition of a class member? (choose three)

a. default

b. public

c. private

d. global

e. common

f. protected

16. Given the following class definition, choose the answer that shows the correct definitions of
the getters and setters for the attributes of the class:

class USImmigrant{
String name;
boolean terrorist;
}

a. public void getName()

public void isTerrorist()

public void setNume(String n)

public void setTerrorist(boolean b)

b. public void getName(String n)

public void isTerrorist(boolean b)

public String setName(String n)

public boolean setTerrorist(boolean b)

c. public String getName(String n)

public boolean getTerrorist(boolean b)

public String setName(String n)

public boolean setTerrorist(boolean b)

d. public boolean getName()

public boolean isTerrorist()

public String setName(boolean n)

public boolean setTerrorist(String t)

e. public String getName()

public boolean isTerrorist()

public void setName(String n)

public void setTerrorist(boolean b)

f. no answer

Page 12 of 39
17. How can delete an object created in Java?

a. by manually calling the finalize() method of the object

b. by the Java garbage collector

c. by using a class destructor

d. Java objects are only destroyed when application exists

e. no answer

18. Which of the following are considerate to be members of a Java class? (choose two)

a. methods

b. attributes

c. qualifiers

d. arguments

e. return types

f. modifiers

19. What is a reason for writing an overloaded method?

a. to provide the user of the method alternative ways to call the method depending on the
type of data the user wants to provide the method

b. to allow the user the method a way to change the name of the arguments when calling
the method

c. to allow the same task to be called by different names

d. to provide a way for the same method to return a variety of data types

e. no answer

20. Given the following piece of code, how can method2() nee invoked on line 3? (choose two)

class Methods{ //1


void method1(){ //2
??? //3
} //4
void method2(){} //5
} //6

a. Methods.method2()

b. this->method2()

c. method2()

d. this.method2()

e. method1().method2()

21. Given the method definition below, what type of data may the programmer return from the
method?

long f(){
//method body
}

a. long only

b. byte, short, int, long, boolean, String

c. byte, short, int, long, char

d. any numeric primitive type

e. no answer

Page 13 of 39
22. What is a class?

a. an object identifier

b. the specification of a new data type

c. an object reference

d. a specific instance of an object

e. no answer

23. The process of creating an object based on a class definition is called:

a. encapsulation

b. access control

c. instantiation

d. overloading

e. garbage collection

f. no answer

24. What will the following code display when executed?

class Test{

int x;

void method1(int x){

x=8;

method2(this.x);

void method2(int x){

x++;

public static void main(String[] s)

Test t = new Test();

t.method1(4);

System.out.println(t.x);

a. 0

b. 9

c. 1

d. 8

e. 4

f. no answer

25. What MUST all method definitions have?

a. a quantifier and return type

b. a name and argument

c. a name, return type and argument list

d. a name and return type

e. a qualifier and argument

f. no answer

Page 14 of 39
Examen Curs 4 (Java Programmer)

1. Which of the following are true about attribute maxLength of class Question below? (choose
three).

class Question{

public static int maxLength;

a. each object of the class its own of the maxLength attribute

b. it must have been initialised when declared, therefore the above code won’t compile

c. it will be created when class Question is loaded by the virtual machine

d. it is accessible to all objects of the class Question

e. it will be created when the first object of the class Question is created

f. it can be accessed using the syntax Question.maxLength

2. Given the following string:

String s = “one two three”;

which of the following pieces of code produce a different result than the initial string?

a. String[] ss s.split(“ “);

System.out.println(ss[1]);

b. System.out.println(s.replace(“one”,””).replace(“ three”,””));

c. System.out.println(s.substring(4,6));

d. Scanner sc = new Scanner(s);

sc.next();

System.out.println(sc.next());

e. System.out.println(s.substring(s.indexOf(“ “)+1, s.lastIndexOf(“ “)));

f. no answer

3. What will the following class display when run?

class Test{
int x = f();
static int y = g();
public Test(){
System.out.println(“A”);
}
int f(){
System.out.println(“B”);
return 0;
}
static int g(){
System.out.println(“C”);
return 1;
}
public static void maiun(String[] s){
new Test();
}

Page 15 of 39
}

a. AB

b. BCA

c. ACB

d. ABC

e. CBA

f. BA

g. no answer

4. Please choose the answer that correctly matches each class with its correct description:

1. String

2. StringBuffer

3. StringBuilder

a. its objects can be modified but they are not thread-safe

b. its objects are immutable

c. its objects can be modified and be safely used in multi-thread applications

a. 1-b, 2-a, 3-c

b. 1-a, 2-b, 3-c

c. 1-b, 2-c, 3-a

d. 1-c, 2-b, 3-a

e. no answer

5. Choose the answer that correctly fills the blank:

The contents of a ______ object cannot be changed once the object has been created.

a. Integer

b. StringBuilder

c. String

d. StringBuffer

e. no answer

6. Which of the following are Java wrapper classes? (choose three)

a. String

b. Integer

c. Char

d. Boolean

e. Int

f. Character

7. Which of the following methods is present in the StringBuffer class but not in the StringBuilder
class?

a. indexOf()

b. delete()

c. charAt()

d. length()

e. nom answer

8. Which of the following are true about static methods of a class? (choose three)

a. they can directly access instance attributes of the same class using this

b. they can’t be called from static initialiser blocks

c. the can directly call instance methods of the same class using their short name

d. they can directly access static members of the class using their short name

Page 16 of 39
e. they can directly call other static methods of the class using their short name

f. they can be called using the syntax ClassName.methodName()

9. What will the following piece of code display when run?

String s1 = “ABC”;
String s2 = new String(“ABC”);

if(s1==s2){
System.out.println(“A”);
}
if(s1.equals(s2)){
System.out.println(“B”);
}
if(s1.compareTo(s2)==0){
System.out.println(“C”);
}

a. ABC

b. A

c. B

d. BC

e. AC

f. AB

g. C

h. nothing

i. no answer

10. Which of the following is NOT true about static initialiser blocks?

a. they can directly call static methods of the class using their short name

b. a class may have multiple initialiser blocks

c. they can directly access instance members of the class using their short name

d. the static attributes that such a block accesses must be declared before the block
instance

e. when using static initialiser blocks in the same class, their order matters

f. no answer

11. Given the following piece of code:

enum Numbers(ONE, TWO, THREE)


class A{
void f(Numbers n){
swithc(n){
case ONE: System.out.println(“You got one!”);
default: System.out.println(n.name());
}
}
}

which of the following statements is true?

a. code will compile successfully

b. code will not compile because enum types cannot be used with switch

c. code will not compile because switch statement doesn’t contain any break statement

d. code will not compile because case ONE should have been written case Numbers.ONE

e. code will not compile because enum types do not have a name() method

f. no answer

Page 17 of 39
12. What will the following piece of code display when executed?

String s = “Abracadabra”;
System.out.println(s.substring(0,5).replace(‘a’,’o’).lastIndexOf(‘a’));

a. -1

b. 0

c. 5

d. 3

e. code will not compile

f. no answer

13. In the code below how a String object be created based on the primitive value int referred by
i? (choose two correct replacements for the ??? sequence)

int i = 5;
String s = ??? ;

a. String.intValue(i)

b. Integer.toString(i)

c. (String)i

d. String.ValueOf(i)

14. Given the Java enumerated type below, which of the following statements is FALSE?

enum RGB {RED, GREEN, BLUE}

a. the RGB class has a static values() method that return an array

b. it is wrong to compare two RGB values using ==. The method equals() must be used
instead

c. the second constant in the list can be accessed using RGB.GREEn

d. RED, GREEN and BLUE are public static final attributes of the RGB enumerated type

e. RGB.BLUE.ordinal() will return 2

f. no answer

15. In the code below, how can an Integer object be created based on the String object referred
by s? (choose the correct replacement for the ??? sequence)

String s = “123”;
Integer i = ??? ;

a. s.parseInt()

b. String.valueOf(s)

c. Integer.valueOf(s)

d. Integer.fromString(s)

e. no answer

16. Which of the following are true about the constructor of a Java enumerated type? (choose
two)

a. the programmer is not allowed to write his own contractor for an enum

b. a contractor written by the programmer inside an enum must have private or

c. an enum always has at least one constructor

d. if the programmer doesn’t write a constructor for an enum, the compiler will provide a

e. a constructor written by the programmer inside an enum must have public access

Page 18 of 39
17. In the code below, how a double primitive value be created based on the String object
referred by s? (choose the correct replacement for the ??? sequence)

String s = “15.2”;
double d = ??? ;

a. Double.doubleValue(s)

b. Double.parseDouble(s)

c. (double)s

d. String.valueOf(s)

e. no answer

18. Which Java class contains static methods for rounding and truncating numbers?

a. Math

b. Number

c. Integer

d. Double

e. no answer

19. In the code below, how can an int primitive value be created based on the Integer object
referred by i? (choose two correct replacements for the ??? sequence)

Integer i = new Integer(8);


int x = ??? ;

a. i.intValue()

b. i

c. i.toInt()

d. Integer.toInt(i)

e. new int(i)

20. In the code below, how can an Integer object be created based on the primitive value i?
(choose three correct replacements for the ??? sequence)

int i = 8;
Integer x = ??? ;

a. Integer.parseInt(i);

b. new Integer();

c. i

d. i.intValue()

e. Integer.intValue(i)

f. Integer.valueOf(i)

21. Which of the following is true about the code below?

class Finals{ //1

final StringBuffer s; //2

Finals(){ //3

s = new StringBuffer(“test”); //4

s.append(“ing”); //5

} //6

} //7

a. the code will bot compile because of an error on line 2: references can’t be declared

b. the code will compile successfully

c. the code will not compile because of an error on line 5: object s is final and therefore it
can not be modified

Page 19 of 39
d. the code will not compile because of an error on line 4: it is too late to initialise s - it can
not be changed any more

e. the code will not compile because of an error on line 4: there is no such StringBuffer
constructor

d. no answer

22. Which of the following is NOT true about Java enumerated type?

a. they can have attributes (which must be declared final) and programmer-defined
methods

b. they have a set of predefined methods

c. they constructors must have private or default access level

d. they define new data type and act as classes that have a limited set of instances

e. they can be instantiated outside the enum definition by using the new operator

f. no answer

23. Where can Java enumerated type be declared? (choose two)

a. outside other classes

b. as a member of a Java class

c. inside a method of a Java class

d. inside a control structure (if, while, etc.)

24. In the code below, how can a String object be created based on the Integer object referred by
i? (choose the correct replacement for the ??? sequence)

Integer i = new Integer(8);


String s = ??? ;

a. s.toString(i)

b. Integer.stringValue(i)

c. i.toString()

d. (String)i

e. i.stringValue()

f. no answer

Examen Curs 5 (Java Programmer)

1. Which of the following commands will run the app.jar archive that exists in the current
directory? (assume that the main class has set correctly)

a. jar -java app.jar

b. jar -f app.jar

c. java -cp app.jar

d. java -jar app.jar

e. no answer

2. All of the following pieces code will create a Period object that encapsulates 2 years, 4 months
and 15 days, EXCEPT: (choose the one that doesn’t).

a. Period p = Period.ofYears(2).ofMonths(4).ofDays(15);

b. LocalDate d1 = LocalDate.of(2016,1,1);

LocalDate d2 = LocalDate.of(2018,5,16);

Period p = Period,between(d1,d2);

c. Period p = Period.ofYears(2).withMonths(4).withDays(15);

d. LocalDate d1 = LocalDate.of(2016, 1, 1);

LocalDate d2 = LocalDate.of(2018,5,16);

Period p = d1.until(d2);

e. Period p = Period,ofYears(2).plusMoths(4).plusDays(15);

Page 20 of 39
3. Which of the following are true about the Month and DayOfWeek types? (choose two)

a. they are enumerated types

b. they both have a getDisplayName() method that can provide the name of the month/day
of the week in the desired language

c. Month.JANUARY and DayofWeek.MONDAY both have the corresponding value 0

d. Month.MAY and DayOfWeek.SUNDAY are values of type int

e. they are part of the java.util package

4. Which of the following characteristics are stored in a Locale object? (choose two)

a. city

b. language

c. number format

d. data format

e. country

5. Which of the following is NOT an advantage of using jar archives?

a. the JVM understand the jar format, therefore classes can be read directly from a jar
archive

b. the jar utility creates a jar file for every .class file, therefore reducing the space copied by
classes

c. the jar format supports compression of Java files and resources, therefore reducing
occupied space and download times

d. an application packaged in a jar archive can be run directly from the archive, without
first extracting it

e. easy distribution of Java applications

f. no answer

6. The JVM needs to load the class having the following definition:

package one.two;

class Load{}

The class path value is “c\projects\javaclasses;d:\java\lib”. Which of the following files may the
virtual machine search for and load, if found? (choose two)

a. c:\projects\javaclasses\two\one\Load.class

b. d:\java\lib\two\one\Load.class

c. c:\projects\javaclasses\one\two\Load.class

d. c:\projects\javaclasses\two\one\Load.java

e. d:\java\lib\one\two\Load.class

f. d:\java\lib\one\two\Load.java

7. What is the real path to the demo.xml file if the code below can correctly find and display the
path to the file? Assume that the value of the class path setting is d:\java\lib.

class ResourceTest{

public static void main(String[] args){

System.out.println(ResourceTest.class.getResource(“/files/xml/demo.xml”));

a. d:\files\mxl\demo.mxl

b. d:\java\lib\files\xml\demo.xml

c. d:\java\files\xml\demo.xml

d. the path cannot be determinate, because it depends on the location of the


ResourceTest class relative to the road of the classpath

e. no answer

Page 21 of 39
8. Given the statement below:

LocalDate ld = ????;

what could replace the ???? sequence in order to create an object that encapsulates the current
date?

a. LocalDate.this()

b. LocalDate.current()

c. LocalDate.present()

d. LocalDate.currentDate()

e. LocalDate.now()

f. new LocalDate()

g. no answer

9. Which of the following show correct ways of calling the getResource() method? (choose three)

a. class FindResource{

public static void main(String[] args){

URL u = this.getClass().getResource(“/pic.jpg”);

b. class find Resource{

public static void main(String[] args){

URL u = FindResource.class.getResource(“/pic.jpg”);

c. class FindResource{

public void test(){

URL u = this.class().getResource(“pic.jpg”);

d. class FindResource{

public void test(){

URL u = FindResource.getClass().getResource(“/pic.jpg”);

e. class FindResource{

public void test(){

URL u = FindResource.getClass().getResource(“/pic.jpg”);

f. class FindResource{

public void test(){

URL u = this.getClass().getResource(“/pic.jpg”);

10. A Java programmer needs to use the following classes in one of his own classes:
java.lang.Math, java.awt.Button and java.awt.event.ActionEvent. What is the minimal set of import
statements that he needs to use at the beginning of the file?

a. import java.awt.Button; import java.awt.event.ActionEvent;

b. import java.awt.*;

c. import java.awt.*; import java.lang.*;

d. import java.lang.Math; import java.awt.event.ActionEvent;

e. import java.*;

f. no answer

Page 22 of 39
11. Which of the following statements will correctly compile and create a LocalDateTime object
that encapsulates January 14, 2016 11:23:51? (choose three)

a. LocalDateTime.from(2016, 1, 14).with(LocalTime.of(11,23,51))

b. LocalDate.of(2016, 1, 14).until(LocalTime.of(11, 23, 52))

c. LocalDateTime.of(2016, 1, 14, 11, 23, 51)

d. LocalDate.of(2016, 1, 14).atTime(LocalTime.of(11, 23, 51))

e. new LocalDateTime(2016, 1, 14, 11, 23, 51)

f. Year.of(2016).atMonth(Month.JANUARY).atDay(14).atTime(LocalTime.of(11, 23, 51))

12. Which Java package is automatically imported?

a. java.lang

b. java.util

c. java.io

d. java.sql

e. no answer

13. What should be used in the source file that contains a class definition in order to make the
class part of a package?

a. an import statement as the first line insider the class constructor

b. a package statement at there beginning of the source file

c. the full class name when declaring the class (class packageName, class{……et…..})

d. an import statement at the beginning of the source file

e. no answer

14. What import statement should a programmer write if he/she intends to use the Year and
LocalDateTime classes?

a. import java.time.*;

b. import java.lang.*;

c. import java.util.*;

d. all of the above are necessary;

e. no answer

15. What method name should replace the ???? in the code in order to create a new object that
encapsulates the data January 14, 2016?

LocalDate d = LocalDate.????(2016, 1, 14);

a. at

b. with

c. from

d. of

e. set

f. no answer

16. Given the hierarchy of resources above, which of the


following are correct values that can be assigned to variable
path in the code below in order to access the pic1.jpg file?
(choose two)

package classes;

class Images{

public static void main(String[] args){

String path = ????;

URL pic1 = Images.class.getResource(path);

Page 23 of 39
a. ../classes/pictures/pic1.jpg

b. /pictures/pic1.jpg

c. pictures/pic1.jpg

d. ../pictures/pic1.jpg

e. ../classes/pic1.jpg

f. /classes/pictures/pic1.jpg

17.Which of the following is a benefit of using packages in Java?

a. minimising or avoiding name collisions

b. a class member with default access level will be hidden to classes from the same
package but available to outside classes

c.a class member with protected access level will only be accessible in subclasses from
the same package

d. less structuring for the projects

e. no answer

18. Given a class named Example that is part of the package test.classes, which of the following
class definitions will correctly compile? (choose two)

a. package mypackage;

class MyClass{

Example e = new Example();

b. package mypackage;

class MyClass{

test.classes.Example e = new Example();

c. package mypackage;

import test.classes.Example;

class MyClass{

Example e = new Example();

d. package mypackage;

import test.*;

class MyClass{

Example e = new Example();

e. package mypackage;

import test.classes.*;

class MyClass{

test.classes.Example e = new Example();

f. package mypackage;

import test.classes;

class MyClass{

Example e = new Example();

19. Given the following piece of code:

LocalDate d = LocalDate.of( 2016, 8, 12);

String fmt = “dd ???? YYYY”;

System.out.println(d.format(DateTimeFormatter.ofPatteern(fmt)));

what should replace the ???? sequence in order for the code to display the short name of the
month?

a. MMMM

b. M

c. MMM

Page 24 of 39
d. MM

e. no answer

20. Which of the following classes could be used for storing an amount of time totalling 30 hours,
20 minutes and 5 seconds?

a. LocalDateTime

b. LocalTime

c. Period

d. Duration

e. no answer

21. What is a Java Locale object?

a. an object that contains number formats, date formats, currencies etc. for a certain
country

b. an object that encapsulates a local date and time

c. an object that identifies a geographical/cultural area

d. an object that contains a group of resources (e.g. translated messages) that depend on
the language and country

e. no answer

22. Given the Java class definition below, which of the following statements placed on line X will
make the code compile correctly? (choose three)

import static java.lang.Integer.*;


class Test{
public static void main(String[] args){
//——————————————-> line X
}
}

a. System.out.prinln(intValue(“5”));

b. System.out.println(valueOf(MAX_VALUE));

c. System.out.println(MAX_VALUE-1);

d. System.out.println(parseInt(“16”));

e. Integer i = new Integer(PI);

f. the code shown already contains an error and will never compile, no matter what is put
on line X

23. Given a reference to a YearMonth object named ym, which of the following pieces of code will
produce another YearMonth object that encapsulates the same month but three years later?
(choose two)

a. Period p = Period.ofYears(3);

YearMonth ym1 = ym.plus(p);

b. YearMonth ym1 = Year.plus(3).of(ym);

c. YearMonth ym1 = ym.withYears(3);

d. YearMonth ym1 = ym.plusYears(3);

e. Duration d = Duration.ofYears(3);

YearMonth ym1 = ym.of(d);

24. Which of the following pieces of code will sign the current month to variable x? (choose four)

a. Month x = Period,now().getMonth();

b. Month x = Duration.now().getMonth();

c. Month x = YearMonth.now().getMonth();

d. Month x = Year.now().getMonth();

e. Month x = MonthDay.now().getMonth();

Page 25 of 39
f. Month x = LocalDate.now().getMonth();

g. Month x = LocalDatetTime.now().getMonth();

h. Month x = Month.now().getMonth();

Examen Curs 6 (Java Programmer)

1. Given the set of classes below, which of the following statements is a valid assignment?

class Phone{}
class MobilePhone extends Phone{}
class SmartPhone extends MobilePhone{}

a. SmartPhone s = new MobilePhone();

b. SmartPhone s = new Phone();

c. MobilePhone m = new Phone();

d. Phone p = new MobilePhone();

e. no answer

2. How can the super keyword be used? (choose three)

a. for accessing non-private fields of the parent class from the subclass

b. for calling a constructor of a subclass from another constructor of the same subclass

c. for accessing the parent method implementation from a subclass implementation in the
case of static methods

d. for setting the most permissive access level for a method that will be inherited in
subclasses

e. for calling the parent constructor from a subclass constructor

f. for calling a parent method implementation from the overriden subclass implementation
in the case of instance methods

3. If a method of a parent class must be accessible in subclasses but not overidden in them, what
keyword must be used in the method definition?

a. abstract

b. private

c. static

d. final

e. no answer

4. Given the code fragment below, which of the following expressions evaluates as false on line
X?

class A{}
class B extends A{}
class C extends B{}
class D extends B{}

class Inheritance{
public static void main(String[] args){
A a = new A();
B b = new B();
C c = new C();
D d = new D();
// ----------------> line X
}

Page 26 of 39
}

a. c instanceof Object

b. d instanceof C

c. c instanceof B

d. a instanceof A

e. d instanceof A

f. b instanceof A

g. no answer

5. What will the following piece of code display when executed?

class A{
void m(){ System.out.println("A"); }
}
class B extends A{
void m(){ System.out.println("B"); }
}
public class C extends B{
void m(){ System.out.println("C"); }
public static void main(String[] args){
B b1 = new C();
b1.m();
}
}

a. A

b. C

c. B

d. the code doesn't compile

e. no answer

6. What modifiers and qualifiers does an interface attribute automatically have, even if the
programmer doesn't explicitly specify them? (choose three)

a. final

b. transient

c. protected

d. abstract

e. public

f. static

7. What access level must a parent class member have in order to be accessible only in
subclasses from the same package?

a. default

b. public

c. protected

d. private

e. no answer

8. Which of the following is NOT true about a java interface?

a. it is declared using the keyword interface

b. it can have static attributes

c. it may have constructors

d. all of its methods are abstract

e. it can be implemented by concrete or abstract classes

f. no answer

Page 27 of 39
9. Given the following class definitions that are part of different packages, which members of the
Parent class will be accessible in the methods of Subclass?

// class Parent is part of package alpha
public class Parent{
public int a;
private int b;
protected int c;
int d;
}

// class Subclass is part of package beta
public class Subclass extends Parent{
void method(){
?????? // which members of Parent are accessible here?
}
}

a. a and d only

b. a, b and c

c. a, c and d

d. a and c only

e. a only

f. a and b only

g. no answer

10. Which of the following is true about an abstract Java class?

a. it can be instantiated

b. it cannot be extended by another class

c. it only has abstract methods

d. it has a constructor

e. no answer

11. Which of the following is true about the code fragment below?

class TShirt{
protected String size;
public TShirt(String s){ size = s; }
}
class RockTShirt extends TShirt{
String band;
public RockTShirt(String s, String b){
size = s;
band = b;
}
}

a. the code will not compile because the subclass constructor is missing the call to the
parent constructor
b. the code will not compile because attribute size is not accessible inside the subclass
c. RockTShirt is the parent class of TShirt
d. the code will compile successfully
e. a TShirt object "is a" RockTShirt
f no answer

Page 28 of 39
12. Given the following set of classes, which of the following statements is an invalid way to call
method f() on line X?

class Phone{}
class MobilePhone extends Phone{}
class SmartPhone extends MobilePhone{}

class Test{
void f(MobilePhone p){}
public static void main(String[] args){
Test t = new Test();
MobilePhone p1 = new MobilePhone();
SmartPhone p2 = new SmartPhone();
MobilePhone p3 = new SmartPhone();
Phone p4 = new MobilePhone();
t.f(????????); // <---------------------------- line X
}
}

a. t.f(p2)
b. t.f(p3)
c. t.f(p4)
d. t.f(p1)
e. no answer

13. What is true about the following code?

class Router{
private String model;
public Router(String m){ model = m; }
}
class WirelessRouter extends Router{
String supportedWirelessStandard;
public WirelessRouter(String w){
 supportedWirelessStandard = w;
}
}

a. the code will not compile because a parent class cannot have private attributes
b. the code will not compile because the subclass constructor must have at least two
arguments
c. the code will not compile because a subclass is not allowed to declare any new
attributes
d. the code will not compile because the subclass constructor doesn't include an explicit
call to the parent constructor
e. the code will compile successfully
f. no answer

14. Which of the following is NOT a benefit of inheritance?

a. the possibility to write multiple methods with the same name in the same class

b. the possibility to use an object of the subclass where an object of the parent class is
required

c. code reuse

d. the possibility to create related data types

e. no answer

Page 29 of 39
15. Given the following class definitions, what methods can be called for reference q on line X?
(choose two)

class Question{
public void getNumAnswers(){}
}
class MultipleChoiceQuestion extends Question{
public Answer[] getCorrectAnswers(){}
public static void main(String[] args){
Question q = new MultipleChoiceQuestion();
q.?????(); // <-------------------------------------- line X
}
}

a. getNumAnswers()
b. toString()
c. isEqual()
d. getCorrectAnswers()

16. Which method from class Object must be overridden in the Person class in order for the code
below to print John?

Person p = new Person("John");
System.out.println(p);

a. equals()

b. hashCode()

c. stringValue()

d. isEqual()

e. toString()

f. no answer

17. Which of the following methods from the Object class returns boolean?

a. hashCode()

b. isEqual()

c. toString()

d. compareTo()

e. equals()

f. no answer

18. Which of the following are true about inheritance in Java? (choose two)

a. when a class extends Object it can't extend any other class

b. a class may extend several other classes but it may implement exactly one interface

c. a class may only extend one other class but it may implement several interface

d. any Java class automatically extends Object


e. an abstract parent class is implemented by subclasses, not extended

19. Given the set of classes below, which of the following assignments will successfully compile
and run (without generating a compilation or runtime error) when inserted on line X?

class Phone{}
class MobilePhone extends Phone{}
class SmartPhone extends MobilePhone{}
public class Casts{
public static void main(String[] args){
Page 30 of 39
MobilePhone p1 = new MobilePhone();
SmartPhone p2 = new SmartPhone();
MobilePhone p3 = new SmartPhone();
Phone p4 = new MobilePhone();
?????????? // <-------------------------- line X
}
}

a. p1 = (MobilePhone)p2;

b. p3 = (SmartPhone)p4;

c. p4 = (SmartPhone)p1;

d. p3 = (SmartPhone)p1;

e. p2 = (MobilePhone)p1;

f. no answer

20. Which of the following keywords are used for creating an "is a" relationship between two
classes or between a class and an interface? (choose two)

a. derivates

b. enhances

c. implements

d. extends

21. When overriding a method in a subclass, what difference may the subclass method signature
have compared to the parent method?

a. a more permissive access level

b.  a changed list of argument data types

c. a "bigger" return type (ex: returns int instead of byte)

d. another method name

e. a "smaller" return type (ex: returns byte instead of int)

f. no answer

22. Which of the following statements inserted on line X will make the code below display true?

class Pet{
String name;
public boolean equals(Object o){
// -------------------------------> line X
}
public static void main(String[] args){
Pet p1 = new Pet();
p1.name = "Lassie";
Pet p2 = new Pet();
p2.name = "Lassie";
System.out.println(p1.equals(p2));
}
}

a. return this.equals((Pet)o);

b. return this.equals(o);

c. return super.equals(o);

d. return name.equals(o.name);

e. return name.equals(((Pet)o).name);

f. return o.equals((Pet)name);

g. no answer

Page 31 of 39
23. What modifiers and qualifiers does an interface method automatically have, even if the
programmer doesn't explicitly specify them? (choose two)

a. protected

b. final

c. public

d. abstract

e. static

24. Which of the following is true about redeclaring parent class attributes in subclasses?

a. the attribute in the subclass must have a different name (identifier) from the one in the
parent class

b. the attribute in the subclass must have the same value as the one in the parent class

c. the attribute in the subclass must have the same access level as the one in the parent
class

d. when accessed using its short name, the attribute in the subclass will hide (shadow) the
one in the parent class

e. the attribute in the subclass must have the same data type as the one in the parent
class

f. no answer

25. Which Java operator is used for testing the "is a" relationship between classes?

a. instanceof

b. is_a

c. implements

d. extends

e. subclasses

f. no answer

Examen Curs 7 (Java Programmer)

1. Please choose the answer that correctly matches the following elements of the AWT/Swing
event system with their description:

a. event source

b. event object

c. listener object

d. listener interface

1. it must be implemented by the object that handles the event

2. it contains the details about the interaction between the user and the GUI

3. it is the component that generates the event

4. it contains the code that gets executed as a reaction to an event

a. 1-d, 2-c, 3-b, 4-a

b. 1-d, 2-c, 3-a, 4-b

c. 1-a, 2-b, 3-c, 4-d

d. 1-d, 2-b, 3-a, 4-c

e. no answer

2.Which of the following is NOT a graphical component but is often used as part of a GUI?

a. JTextField

b. JTable

c. ButtonGroup

d. JComboBox

Page 32 of 39
3. Which of the following is a semantic event?

a. KeyEvent

b. MouseEvent

c. ActionEvent

d. none of the above

e. no answer

4. How many .class files will the following code generate when compiled?

public class A{
public void f(){}
}
class B{
int x = 8;
void g(){
A a1 = new A(){
public void f(){
System.out.println(x);
}
};
}
}
}

a. 2

b. 3

c. 0

d. 1

e. no answer

5. Which of the following is a method that all AWT/Swing event objects have?

a. getModel()

b. getSource()

c. getActionCommand()

d. getModifiers()

e. no answer

6. Which of the following is true about the piece of code below?

public class A{
public void f(){}
}
class B{
void g(){
int x=3;
A a1 = new A(){
public void f(){
System.out.println(x);
}
};
}
}

Page 33 of 39
a.  the code will not compile because local variable x should have been declared final
b. the code will not compile because anonymous inner classes cannot be created inside
class methods

c. the code will not compile because an inner class can never access the local variables of
the method it has been declared in

d. the code will compile successfully

e. no answer

7. What is true about the piece of code below?

class A{

private int x;

class B{

public void f(){

x++;

a. the code will not compile because method f() cannot access a private variable of the
outer class

b. the code will not compile because method f() can access variable x only if x has been
declared final

c. the code will compile successfully

d. the code will not compile because class B should be declared outside class A

e. the code will not compile because inner classes can only be anonymous

f. no answer

8. What kind of event does a JComboBox generate when the user selects a different value from
the drop-down list?

a. ActionEvent

b. CaretEvent

c. DocumentEvent

d. WindowEvent

e. no answer

9. What is the parent class of all graphical Swing components that have button-like behaviour?

a. AbstractButton

b. JButton

c. Button

d. JMenuItem

e. no answer

10. What is a special feature of JDialog compared to JFrame?

a. a JDialog can be resized, while a JFrame can not

b. a JDialog can have a menu bar, while a JFrame can not

c. a JDialog can be modal, while a JFrame can not

d. JDialog is a root container, while JFrame is not

e. no answer

11. A group of Swing buttons that exclude each other must all have the same:

a. button group

b. size

c. alignment

Page 34 of 39
d. name

e. text

f. no answer

12. Which of the following are true about the AWT/Swing event system? (choose TWO)

a. when pressed once, a button generates a series of events, not just one

b. a component can usually generate several types of events

c. a component may only have one listener object per event type

d. every listener interface has exactly one method that must be overridden by listener
objects

e. all event objects have the same set of methods, but with different implementations

13. What kind of event can be generated by both JButtons and JComboBoxes?

a. WindowEvent

b. ActionEvent

c. DocumentEvent

d. CaretEvent

e. no answer

14. Which of the following will correctly display a popup dialog that displays an error message?
Assume that wnd is a reference to an existing JFrame.

a. JDialog.showMessage(wnd, "An error has occured", "ERROR", ERROR_MESSAGE);


b. JFrame.showDialog(wnd, "An error has occured", "ERROR",
JFrame.ERROR_MESSAGE);

c. wnd.showDialog("An error has occured", "ERROR", JDialog.ERROR_MESSAGE);


d. JOptionPane.showMessageDialog(wnd, "An error has occured" ,"ERROR",
JOptionPane.ERROR_MESSAGE);

e. no answer

15. Which of the following correctly creates and displays a JFrame which contains a button that
closes the application? (choose TWO)

a. public class TheFrame {

public static void main(String[] args) {

JFrame f = new JFrame();

       JButton b = new JButton(“Exit");

   b.addActionListener(new Exit());

f.add(b);

f.setSize(200,40);

f.setVisible(true);

class Exit implements ActionListener{

     public void actionPerformed() {

         System.exit(0);

     }

    

  b. public class AFrame {

      public static void main(String[] args) {

         JFrame f = new JFrame();

         JButton b = new JButton("Exit");

         f.add(b);

Page 35 of 39
         f.setSize(200,40);

         f.setVisible(true);

     }

class Exit implements ActionListener{

     public void actionPerformed(ActionEvent e) {

         System.exit(0);

     }

    

  c. public class AFrame {

     public static void main(String[] args) {

         JFrame f = new JFrame();

         JButton b = new JButton("Exit");

         b.addActionListener(new Exit());

         f.add(b);

         f.setSize(200,40);

         f.setVisible(true);

     }

class Exit{

     public void actionPerformed(ActionEvent e) {

         System.exit(0);

     }

    

  d. public class Frame {

     public static void main(String[] args) {

         JFrame f = new JFrame();

         JButton b = new JButton("Exit");

         b.addActionListener(new ActionListener() {

             public void actionPerformed(ActionEvent e) {

                 System.exit(0);

             }

         });

         f.add(b);

        f.setSize(200, 40);

         f.setVisible(true);

     }

  e. public class Frame {

     public static void main(String[] args) {

         JFrame f = new JFrame();

         JButton b = new JButton("Exit");

         b.addActionListener(new Exit());

         f.add(b);

         f.setSize(200,40);

         f.setVisible(true);

     }

class Exit implements ActionListener{

     public void actionPerformed(ActionEvent e) {

         System.exit(0);

Page 36 of 39
     }

    

16. Given the following code:

class Combo {
     public static void main(String[] a) {
         JFrame f = new JFrame();
         JComboBox cmb = new JComboBox();
         String[] elements = {"one", "two", "three"};
         // ---------- line X ------------
         f.add(cmb);
         f.setSize(200,30);
         f.setVisible(true);
     }
}

which of the following could be inserted on line X so that the code runs successfully and displays
the window below? (choose TWO)

a. DefaultComboBoxModel model = new DefaultComboBoxModel();

for(String s:elements){

cmb.setModel(s);

model.addElement(cmb);

b. DefaultComboBoxModel model = new DefaultComboBoxModel();

for(String s:elements){

model.addElement(s);

cmb.setModel(model);

c. DefaultComboBoxModel model = new DefaultComboBoxModel(elements);

cmb.addElement(model);

d. DefaultComboBoxModel model = new DefaultComboBoxModel(cmb);

for(String s:elements){

cmb.addElement(s);

e. cmb.setModel(elements);

f. DefaultComboBoxModel model = new DefaultComboBoxModel(elements);

cmb.setModel(model);

17. The architecture of a Swing component is inspired by the MVC pattern, but it collapses the
following parts into a single UI object:

a. model and view

b. model and controller

c. view and controller

Page 37 of 39
d. model, view and controller

  e. no answer

18. Which Swing components can be shown on the screen without being placed inside another
component?

a. complex components like lists or tables

  b. root containers

  c. basic components like buttons or checkboxes

d. intermediate containers

e. no answer

19. Which of the following statements will color panel p in red? (choose TWO)

  a. p.setBackground(Color.RED);

b. p.setBackground(RED);

c. p.setForeground(new Color(RED));

d. p.setForeground(Color.RED);

e. p.setBackground(new Color(255,0,0));

20. A graphical user interface of a Java application corresponds to which part of the MVC design
pattern?

a. model

  b. view

c. controller

d. none of the above

e. no answer

21. Which of the following types of graphical components can be placed in a button group?
(choose TWO)

a. JSpinner

b. JCheckBox

c. JSlider

d. JRadioButton

e. JComboBox

22. Which of the following Swing components have a text atttribute that can be manipulated using
the component's getText() and setText() methods? (choose THREE)

a. JButton

  b. JProgressBar

  c. JPanel

  d. JList

e. JLabel

f. JCheckBox

23. Which of the following classes correspond to a button or button-like component that doesn't
remain "pressed" after clicking on it?

a. JCheckBoxMenuItem

b. JButton

c. JCheckBox

d. JRadioButton

e. JRadioButtonMenuItem

f. JToggleButton

g. no answer

Page 38 of 39
24. What interface must be implemented by the object that reacts to a Swing button being
pressed?

  a. ButtonListener

b. ActionListener

c. ActionModel

d. CaretListener

e. ActionEvent

f. no answer

24. Which of the following types of panels contains two components separated by a divider,
automatically adjusting the sizes of the components when the user moves the divider?

a. JSplitPane

b. JLayeredPane

c. JTabbedPane

d. JScrollPane

e. no answer

Page 39 of 39

Вам также может понравиться