Examen Curs 1 (Java Programmer) : C. The JVM or Equivalent Emulator
Examen Curs 1 (Java Programmer) : C. The JVM or Equivalent Emulator
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?
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
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
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?
b. a way to reach the location where the string Some Value is stored
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
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
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
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
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
a. an error will be displayed, because a class name shouldn’t start with an uppercase letter
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?
b. a start() function
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?
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
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?
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
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?
e. no answer
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
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
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
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)
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];
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?
b. arr.length++;
e. no answer
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)
‘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?
e. no answer
1. Which of the following are true about the class constructor? (choose three)
a. it cannot be overloaded
d. a class always contains a constructor, even if the programmer doesn’t provide one
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:
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
1. public 3. private
2. default 4. protected
e. no answer
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
a. a mutator is an object method to create and return a new object of the same type
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
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
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
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?
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)
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
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)?
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;
}
f. no answer
Page 12 of 39
17. How can delete an object created in Java?
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
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
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)
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
e. no answer
Page 13 of 39
22. What is a class?
a. an object identifier
c. an object reference
e. no answer
a. encapsulation
b. access control
c. instantiation
d. overloading
e. garbage collection
f. no answer
class Test{
int x;
x=8;
method2(this.x);
x++;
t.method1(4);
System.out.println(t.x);
a. 0
b. 9
c. 1
d. 8
e. 4
f. no answer
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{
b. it must have been initialised when declared, therefore the above code won’t compile
e. it will be created when the first object of the class Question is created
which of the following pieces of code produce a different result than the initial string?
System.out.println(ss[1]);
b. System.out.println(s.replace(“one”,””).replace(“ three”,””));
c. System.out.println(s.substring(4,6));
sc.next();
System.out.println(sc.next());
f. no answer
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
e. no answer
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
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
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
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
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
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
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?
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
d. RED, GREEN and BLUE are public static final attributes of the RGB enumerated type
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
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)
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)
Finals(){ //3
s.append(“ing”); //5
} //6
} //7
a. the code will bot compile because of an error on line 2: references can’t be declared
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
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
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)
a. s.toString(i)
b. Integer.stringValue(i)
c. i.toString()
d. (String)i
e. i.stringValue()
f. no answer
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)
b. jar -f 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);
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)
b. they both have a getDisplayName() method that can provide the name of the month/day
of the week in the desired language
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
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
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{
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
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{
URL u = this.getClass().getResource(“/pic.jpg”);
URL u = FindResource.class.getResource(“/pic.jpg”);
c. class FindResource{
URL u = this.class().getResource(“pic.jpg”);
d. class FindResource{
URL u = FindResource.getClass().getResource(“/pic.jpg”);
e. class FindResource{
URL u = FindResource.getClass().getResource(“/pic.jpg”);
f. class FindResource{
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?
b. import java.awt.*;
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))
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?
c. the full class name when declaring the class (class packageName, class{……et…..})
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.*;
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?
a. at
b. with
c. from
d. of
e. set
f. no answer
package classes;
class Images{
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
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
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{
b. package mypackage;
class MyClass{
c. package mypackage;
import test.classes.Example;
class MyClass{
d. package mypackage;
import test.*;
class MyClass{
e. package mypackage;
import test.classes.*;
class MyClass{
f. package mypackage;
import test.classes;
class MyClass{
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
a. an object that contains number formats, date formats, currencies etc. for a certain
country
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)
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”));
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);
e. Duration d = Duration.ofYears(3);
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();
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{}
e. no answer
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
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
}
b. d instanceof C
c. c instanceof B
d. a instanceof A
e. d instanceof A
f. b instanceof A
g. no answer
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
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
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?
}
}
b. a, b and c
c. a, c and d
d. a and c only
e. a only
g. no answer
a. it can be instantiated
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
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
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
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)
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
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?
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
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
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
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
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
e. no answer
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
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
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
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.
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
}
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:
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?
b. root containers
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
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