Practise3-Sol (New!)

advertisement
CENG 104 B - PRACTICE QUESTIONS #3
Q-1) __________ represents an entity in the real world
that can be distinctly identified.
1)
2)
3)
4)
A(String s) {
this.s = s;
}
A class
An object
A method
A data field
Q-2) _______ is a construct that defines objects of the
same type.
a.
b.
c.
d.
class A {
String s;
A class
An object
A method
A data field
Q-3) An object is an instance of a __________.
void print() {
System.out.println(s);
}
}
a.
b.
c.
d.
a.
b.
c.
d.
program
class
method
data
Q-4) The keyword __________ is required to declare a
class.
a.
b.
c.
d.
public
private
class
All of the above.
Q-5) ________ is invoked to create an object.
a.
b.
c.
d.
A constructor
The main method
A method with a return type
A method with the void return type
Q-6) Which of the following statements are true?
a.
b.
c.
d.
e.
A default no-arg constructor is provided automatically
if no constructors are explicitly declared in the class.
At least one constructor must always be defined
explicitly.
Constructors do not have a return type, not even void.
Constructors must have the same name as the class
itself.
Constructors are invoked using the new operator when
an object is created.
Q-7) Analyze the following code:
public class Test {
public static void main(String[]
args) {
A a = new A();
a.print();
}
}
The program has a compilation error because class A is
not a public class.
The program has a compilation error because class A
does not have a default constructor.
The program compiles and runs fine and prints
nothing.
The program would compile and run if you change A a
= new A() to A a = new A("5").
Q-8) What is wrong in the following code?
class TempClass {
int i;
public void TempClass(int j) {
int i = j;
}
}
public class C {
public static void main(String[]
args) {
TempClass temp = new TempClass(2);
}
}
a.
b.
c.
d.
The program has a compilation error because
TempClass does not have a default constructor.
The program has a compilation error because
TempClass does not have a constructor with an int
argument.
The program compiles fine, but it does not run
because class C is not public.
The program compiles and runs fine.
Q-9) Given the declaration Circle x = new Circle(), which of
the following statement is most accurate.
a.
b.
c.
d.
x contains an int value.
x contains an object of the Circle type.
x contains a reference to a Circle object.
You can assign an int value to x.
Q-10) Analyze the following code.
public class Test {
int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[] args)
{
Test test = null;
System.out.println(test.x);
}
"The default constructor of B is
invoked");
}
}
}
a.
b.
c.
d.
e.
The program has a syntax error because test is not
initialized.
The program has a syntax error because x has not been
initialized.
The program has a syntax error because you cannot
create an object from the class that defines the object.
The program has a syntax error because Test does not
have a default constructor.
The program has a runtime NullPointerException
because test is null while executing test.x.
Q-11) Object-oriented programming allows you to derive
new classes from existing classes. This is
called ____________.
a.
b.
c.
d.
encapsulation
inheritance
abstraction
generalization
public class C {
public static void main(String[] args)
{
B b = new B();
}
}
a.
b.
c.
d.
e.
Q-15) What is the representation of the third element in
an array called a?
a.
b.
c.
d.
Q-12) Which of the following statements are true?
a.
b.
c.
d.
A subclass is a subset of a superclass.
A subclass is usually extended to contain more functions
and more detailed information than its superclass.
"class A extends B" means A is a subclass of B.
"class A extends B" means B is a subclass of A.
Q-13) Suppose you create a class Cylinder to be a subclass
of Circle. Analyze the following code:
class Cylinder extends Circle {
double length;
Cylinder(double radius) {
Circle(radius);
}
none
"The default constructor of B is invoked"
"The default constructor of A is invoked""The
default constructor of B is invoked"
"The default constructor of B is invoked""The
default constructor of A is invoked"
"The default constructor of A is invoked"
a[2]
a(2)
a[3]
a(3)
Q-16) Which of the following is incorrect?
a.
b.
c.
d.
e.
int[] a = new int[2];
int a[] = new int[2];
int[] a = new int(2);
int a = new int[2];
int a() = new int[2];
Q-17) What is the correct term for numbers[99]?
a.
b.
c.
d.
e.
index
index variable
indexed variable
array variable
array
}
a.
b.
c.
The program compiles fine, but you cannot create an
instance of Cylinder because the constructor does not
specify the length of the cylinder.
The program has a syntax error because you attempted
to invoke the Circle class's constructor illegally.
The program compiles fine, but it has a runtime error
because of invoking the Circle class's constructor
illegally.
Q-14) What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is
invoked");
}
}
class B extends A {
public B() {
System.out.println(
Q-18) Suppose int i = 5, which of the following can be
used as an index for array double[] t = new double[100]?
a.
b.
c.
d.
e.
İ
(int)(Math.random() * 100))
i + 10
i + 6.5
Math.random() * 100
Q-19) Analyze the following code.
public class Test {
public static void main(String[] args) {
int[] x = new int[3];
System.out.println("x[0] is " + x[0]);
}
}
a.
The program has a syntax error because the size of
the array wasn't specified when declaring the
array.
b. The program has a runtime error because the
array elements are not initialized.
c. The program runs fine and displays x[0] is 0.
d. The program has a runtime error because the
array element x[0] is not defined.
Q-20) Which of the following statements is valid?
a.
b.
c.
d.
e.
int i = new int(30);
double d[] = new double[30];
int[] i = {3, 4, 3, 2};
char[] c = new char();
char[] c = new char[4]{'a', 'b', 'c', 'd'};
Q-21) How can you initialize an array of two characters to
'a' and 'b'?
a.
char[] charArray = new char[2]; charArray = {'a',
'b'};
b. char[2] charArray = {'a', 'b'};
c. char[] charArray = {'a', 'b'};
d. char[] charArray = new char[]{'a', 'b'};
Q-22) What would be the result of attempting to compile
and run the following code?
public class Test {
public static void main(String[] args) {
double[] x = new double[]{1, 2, 3};
System.out.println("Value is " + x[1]);
}
}
a.
The program has a syntax error because the syntax
new double[]{1, 2, 3} is wrong and it should be
replaced by {1, 2, 3}.
b. The program has a syntax error because the syntax
new double[]{1, 2, 3} is wrong and it should be
replaced by new double[3]{1, 2, 3};
c. The program has a syntax error because the syntax
new double[]{1, 2, 3} is wrong and it should be
replaced by new double[]{1.0, 2.0, 3.0};
d. The program compiles and runs fine and the
output "Value is 1.0" is printed.
e. The program compiles and runs fine and the
output "Value is 2.0" is printed.
Q-23) Assume int[] t = {1, 2, 3, 4}. What is t.length?
a.
b.
c.
d.
0
3
4
5
Download