Você está na página 1de 19

1. Explain the term data abstraction.

Data abstraction is the clear separation between the abstract properties of a data type, and
the concrete details of its implementation. The abstract properties are visible to the client, for
example, if one is using a stack, the user will only see its abstract properties: push, pop, etc. Its
concrete implementation (the way it is written in the program) remains hidden and can change in
time (for example, if one found a faster / more efficient method of implementing such a stack)
2. What are the benefits of abstract data types?
Domain concepts are reflected in the code
The internal complexity of the program remains hidden
Usage of data type is independent from its internal implementation
Provides higher modularity
Increase ease of maintainance
3. What is the effect of the <<< operator?
It is a bitwise operator. N <<< p shifts the bits of N left p positions.

4. What makes the Object Oriented paradigm?


OOP considers both the attributes and the operations as being equally important. The
concepts of objects, classes, message passing, and inheritance form the object-oriented paradigm.
5. Explain the strengths of object orientation.
Information is hiding => post-delivery maintenance is safer.
Development is easier because objects usually have physical counterparts!
Well-designed objects are independent units.
Reduced complexity of the program because the product is usually composed of
independent units
OOParadigm promotes reuse!

6. What is abstraction?
It is the process of filtering out all the non-important characteristics of an object as to get
only the most important ones.
7. What are software objects?
Building blocks of software programs
They model tangible and conceptual things and even processes
8. What are object capabilities?
They have: capabilities and properties (methods and attributes)
They have a state: collection of all objects properties

9. What kind of object capabilities can be defined?


Constructors: establishes initial state of an object
Commands: change objects properties
Queries: provide answers based on objects properties
10. What are object properties?
Properties determine how an object acts. Some may be constant, others variable.
Properties themselves are objects!

11. What kind of object properties can be defined?


Attributes: things that help describe an object
Components: things that are part of the object

Associations: things an object knows about but are not parts of that object
12. How can one get a Java standalone application?
Simply put a main method in the program.
13. What is an object's state?
The state is the collection of all objects properties. It changes if any property changes.
14. Explain the term object class.
It is a category of an object. It defines capabilities and properties common among a set of
individual objects. It defines a template for making object instances.
15. What is the role of encapsulation?
It hides all the internal data of an object. One does not need to know exactly HOW a car
works. He / she just need to be aware of its interface; the rest of its internal implementation can
remain hidden. Thus, internal data cannot be accessed directly from the outside, providing data
security.
16. Explain the terms public view and private view as related to a class.
The public view is the interface. It allows instances to communicate with one another
without knowing a lot about the internal implementation. The interface consists of a list of
capabilities.
The private view is the implementation. These are the internal properties of an object that
help complete their tasks.
17. Explain composition abstraction.
The composition abstraction is a more strict aggregation in the means that an object
which has another object as a property, if deleted, will also delete that property. For example,
take a University which has two properties: a chancellor and a faculty. Even if the university
ceases to exist, the chancellor will still live to see another day; but the faculties will disappear.
18. Explain how can one get from an ADT to Java code?
First one must conceptually create the ADT, then the class diagram, then the class
specification followed by the desired Java code.

19. Explain the steps needed to get from source code to the execution of a Java program.
Java source code -> Java compiler -> byte code -> ClassLoader (Byte code verifier)->
Java interpreter / Just-In-Time compiler -> Runtime System -> Operating System -> hardware
20. What is a java stand-alone application?
A java program which contains a main method?
21. What is an applet?
Its a Java application that is meant to be run from a Web browser.

22. What is the role of the class loader?


The class loader is a program that connects the byte-code of the classes needed to run a
java program.
23. Explain the access modifiers on variables/methods.

24. Give examples of names for variables, methods, classes and constants which obey the
naming convention.
Methods and variables: shootOnSight(), isReady
Classes: AProgram, MyClass, EvilMonster
Constants: A_CONSTANT, H_KEY_FOREVERALONE

25. What are the primitive types in Java?


Boolean, char, byte, short, int, long, float, double, void.

26. Explain the assignment compatibility for primitive types in Java.


Byte->short->int->long->float->double + char, before int.

26. What is type casting? Give 2-3 relevant examples.


Short x;
Int y;
Y = (int) x;
27. What types have wrapper classes and what are the corresponding names of wrapper classes.
Types: byte, short, long, float, double and char
Names: Byte, Short, Long, Float, Double and Char
28. What is boxing and un-boxing?
Boxing is the process of going from a value of a primitive type to an object of its wrapper
class.
29. What kind of comments can be used in Java programs?
Line comments: //
Block Comments: /* */
30. What should one use when comparing the contents of objects?
.equals()
Or rather define own comparison methods for that specific class.

31. Give an example of overflow error in Java.


Int i=0;
While(I != 3)
{

I++;
}
32. What is the effect of break with a label?
It exits a multi-nested loop.

33. What is the effect of continue with a label?


It skips the current iteration of a loop.

34. Explain and exemplify the term constructor in Java.


The purpose of a constructor is to initialize an objects state (usually, in a valid state). It
should not do anything else but create that object (initialize it).
Example: alt dat.
35. Explain and exemplify the term method in Java.
A method will execute an objects behavior. It can change an objects state, report its state,
operate on numbers / text / fields / graphics / hardware, create other objects, call other objects
methods, call a method in the same class (this.method()), call itself (recursion).
36. What is the input and output to a method?
Input: parameters /// Output: return type

37. Provide a template for class code.


Import statements;
// class comments.
Public class Monster{
Attributes;
Constructor();
Constructor(parameters);
Method1();
Method2();
}
38. Why is it useful to overload constructors?
You can initialize whichever variable you want, blabla.
39. What can be an operand to a Java expression?

40. What is taken into account when evaluation an expression?

41. What are the relational operators?

42. Write a relational expression which would evaluate to true if the sum of variables x and y
was equal to the value of a variable z.

43. Bracket the following logical expressions to show the order of evaluation of the operators.
Hence if a is 5, b is 10, c is 15 and d is 0 what are the truth values of the expressions?
(c == (a+b)) - true
(a != 7) - true
(b <= a) - false
(a > 5)
- false
((a+d) >= (c-b)) - true
((d/a) < (c*b)) - true

44. Bracket the following logical expressions to show the order of evaluation of the operators.
Hence if a is 5, b is 10, c is 15 and d is 0 what are the truth values of the expressions?
(((c
(((a
(!(b
(!(a

== (a+b)) || (c == d))) - true


!= 7) && (c >= 6)) || (a+c) <= 20))) - true
<= 12)) && ((a % 2) == 0)) - false
>5)) || (c < a+b)) - true

45. To what do the following expressions evaluate?


17/3

17%3

1/2

1/2*(x+y)

46. Given the declarations:


float x;
int k, i = 5, j = 2;

To what would the variables x and k be set as a result of the assignments


k
x
k
x

=
=
=
=

i/j; i/j; i%j; 5.0/j;-

2
2
1
2.5 (actually it needs a cast)

47. If x has the value 3.5 when the following statement is executed what value would be
assigned to y?
if (x + 1 <= 3.6)
y = 1.0;
else
y = 2.0; - y will be 2.0.

48. In words what do you think the effect of the following statement is intended to be? Why is
the statement syntactically incorrect? How could it be changed to be syntactically correct? Has
the change that you have made ensured that the statement actually carries out the logical effect
you stated at the beginning?
if (x >= y){
sum += x;
System.out.println("x is bigger");}
Else{
sum += y;
System.out.println("y is bigger");}

49. Write an if-else statement which would add a variable x to a variable possum if x is positive
and would add x to negsum if the variable x was negative.
If(x>0)
Possum = possum +x;
Else
Negsum = negsum + x;

50. Expand the solution to the previous question so that if x is positive then a variable
poscount is incremented and if x is negative a variable negcount is incremented. If this was
part of a program what would be sensible initializations to carry out?
If(x>0){
Possum = possum +x;
Poscount++;
}
Else{
Negsum = negsum + x;
Negcount++;
} // yes, poscount and negcount will have to be initialized to 0.

51. It is decided to base the fine for speeding in a built up area as follows - 50 pounds if speed is
between 31 and 40 mph, 75 pounds if the speed is between 41 and 50 mph and 100 pounds if he
speed is above 50 mph. A programmer writing a program to automate the assessment of fines
produces the following statement:
if (speed > 30)
fine = 50;
else if (speed > 40)
fine = 75;
else if (speed > 50)
fine = 100;

Is this correct? What fine would it assign to a speed of 60 mph? If incorrect how should it be
written?
Not correct. It will assign fine = 50 because 60 is indeed over 30.
We could either get rid of the else statements, thus the comparisons will keep on going and
eventually we will get the right value, or do some extra tests in the if statements.

52. Write a nested if-else statement that will assign a character grade to a percentage mark as
follows - 70 or over: A, 60-69: B, 50-59: C, 40-49: D, 30-39: E, less than 30: F.
If(mark >= 70)
Grade = A;
Else if(mark < 70 && mark >=60)
Grade = B;
Etc
53. What is the major difference between a while statement and a do-while statement?
The do while statements executes the block once before checking the condition.

54. What would be output by the following segment of Java?


int i;
i = -12;
do
{
System.out.println( i );

i = i - 1;
}
while (i > 0)

-12
(actually there is a syntax error. Need

; after while).

55. What would be output by the following segment of Java?


int i;
for ( i = 1; i <= 12; i *= 2 )
System.out.println( i );

1,2,4,8
56. What is printed by the following segment of Java?
int i;
for (i=1; i<20; i = i+3)
System.out.println( i );

1,4,7,10,13,16,19
57. What would happen if the i+3 in the update expression were changed to i-3?
Infinite loop.
58. An integer, x has a binary value (using 1 byte) of 10011100. What is the binary value of z
after these statements:
int y = 1 << 7;
int z = x & y;

y is 10000000
=> z will be 10000000
59. What will happen if you compile and execute an application with the following code in its
main() method:
String s = new String( "Computer" );
if( s == "Computer" )
System.out.println( "Equal A" );
if( s.equals( "Computer" ) )
System.out.println( "Equal B" );

EQUALS B

60. Consider this class example:


class MyPoint
{
void myMethod()
{
int x, y;
x = 5; y = 3;
System.out.print( " ( " + x + ", " + y + " ) " );
switchCoords( x, y );
System.out.print( " ( " + x + ", " + y + " ) " );
}
void switchCoords( int x, int y )
{
int temp;
temp = x;
x = y;
y = temp;
System.out.print( " ( " + x + ", " + y + " ) " );
}
}

What is printed to standard output if myMethod()is executed?


(5,3) (3,5) (5,3)

61. If arr[] contains only positive integer values, what does this function do?
public int guessWhat( int arr[] )
{
int x= 0;
for( int i = 0; i < arr.length; i++ ) x = x < arr[i] ? arr[i] : x;
return x;
}
returns the biggest number from the array.

62. Consider the code below:


arr[0] = new int[4];
arr[1] = new int[3];
arr[2] = new int[2];
arr[3] = new int[1];
for( int n = 0; n < 4; n++ )
System.out.println( /* what goes here? */ );

What expression should be written instead /* what goes here? */ to print the number of values in
each row?
Arr[i].length();

63. If size = 4, what is the row by row contents of triArray:


int[][] makeArray( int size)
{
int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{
triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
triArray[i][j] = val++;
}
return triArray;
}
2,3,4,5,6,7,8,9,10
64. Consider the code below:
public static void main( String args[] )
{
int a = 5;
System.out.println( cube( a ) );
}
int cube( int theNum )
{
return theNum * theNum * theNum;
}

What will happen when you attempt to compile and run this code?
It will not work because this is not a procedural programming language. We need to
instantiate an object and use the method cube, or make it static.
65.

What would be printed by the code below if val = 1?

switch( val )
{
case 1: System.out.print( "P" );
case 2:
case 3: System.out.print( "Q" );
break;
case 4: System.out.print( "R" );
default: System.out.print( "S" );
}
PQ no break before that.

66. What would be printed to standard output by the code fragment below?
outer: for( int i = 1; i <3; i++ )
{
inner: for( j = 1; j < 3; j++ )
{
if( j==2 ) continue outer;
System.out.println( "i = " +i ", j = " + j );
}
}
I = 1, j =1;
I = 2, j=1;

67. In Java technology what expression can be used to represent number of elements in an array
named arr ?
Arr.length();
68. What is returned when the method substring(2, 4) is invoked on the string
"example"? Include the answer in quotes as the result is of type String.

am

69. What gets printed when the following program is compiled and run?
public static void main(String args[])
{
int i, j=1;
i = (j > 1)? 2: 1;
switch(i)
{
case 0: System.out.println(0); break;
case 1: System.out.println(1);
case 2: System.out.println(2); break;
case 3: System.out.println(3); break;
}
}
1,2
70. What happens upon invocation of a Java method?
1.
2.
3.
4.
5.

Evaluate arguments from left to right


Push a new stack frame on the call stack
Initialize the parameters
Execute the method
Return from the method

71. What happens when the new Java operator is invoked?


Memory allocation occurs (space for the new object is reserved) and
instance variables are initialized to their default values.
Explicit initialization is performed.
A constructor is executed.
The reference to the object is assigned to the variable.
72. What will happen when the following code snippet gets executed?
BigInteger big1 = new BigInteger("1");
BigInteger big2 = new BigInteger("2");
big1 = big2;
They will both reference to 2. Garbage collector will clean out 1.

73. Is the following code correct and complete? Why?


public int sum2(int[] data)
{
int sum = 0;
for (int i = 0; i <= data.length; i++)
sum += data[i];
return sum;
}
74. How does a String differ from an array of characters?
An array is a reference data type, not an object.
75. What is a Java interface?
An interface is a named collection of method definitions without
implementations.
76. In what does an interface differ from a class?
The methods are not implemented, just named.

77. Describe the two components of an interface definition.


Interface Declaration: defines various attributes to the interface such as name, and
weather it extends another interface.
Interface Body: contains the constant and method declarations for that interface.
78. Describe the restrictions which apply to Java interfaces.
All methods must be abstract instance methods.
All variables must be static final.
No static initialize blocks.

79. When should interfaces be used in Java programs?


To simply clarify the functionality associated with a particular abstraction.
To abstract the functionality of a method and make it more general.
To implement callbacks such as in GUI programming.
To write more general, implementation depending code that is easy to extend and
maintain.
To simulate global constants.
80. What is the purpose of Java packages?
Better organization of classes.
81. What are local variables in Java?
82. What are instance variables in Java?

83. What are class variables in Java?


It is a variable declared static.
84. What does the following expression evaluate to?
-4 >>> 28

85. List the differences between methods and constructors.


Constructors can only be called when instatiating a new object while methods can always
be called.
86. Explain why the following sequence is correct or incorrect
short s = 20;
char c = s;
because it needs a cast.

87. Explain why the following sequence is correct or incorrect


byte b = 20;
char c = b;

88. Explain why the following sequence is correct or incorrect


short s1 = 10;
short s2 = 20;
short result = s1*s2;

Its not initially known whether or not the multiplication will be short. Thus, it needs a cast.

89. Given the following class declaration


public class MyClass
{
public static void main(String arg)
{
MyClass mc = new MyClass( );
System.out.println("First Argument is
}
}

: "+arg[0]);

What happens when you try to compile and run MyClass?


Error. Arg is not an array.

90. What happens when you try to compile and the code below?
public class MyClass
{
static int i = 10;
public static void main(String[] arg)
{
static int i = 20;
System.out.println("i is :"+i);
}
}

It cannot compile because i is static but declared in a method. I must be declared globally if it is
to be static.

91. How many String objects are created when we run the following code? Motivate your
answer.
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;

Only creates 2 objects. Hello and HelloPal


Both s1 and s2 point to the same object.
The same for s3 and s4.
92. What is the output of the following code? Why?
int i = 10;
long l = 10L;
if( i == l )
System.out.println("We are Equal");

Because they are equal?

93. What is the output of the following code? Why?


int i = 10;
char c = 10;
if( c == i)
System.out.println("We are Equal");

They are equal. They point to the same thing.

94. What is the output of the following code? Why?


String s1 = "Null";
String s2 = "Null";
if( s1 == s2)
System.out.println("We are Equal");

They are equal. They point to the same thing.

95. What is the output of the following code? Why?


String s1 = "Null";
String s2 = new String(s1);
if( s1 == s2)
System.out.println("We are Equal");

They are not equal. No output because s2 is an object.

96. What is the output of the following code? Why?


String s1 = "OK";
String s2 = new String(s1);
if( s1.equals(s2))
System.out.println("We are Equal");

They are equal.

97. What is the output of the following code? Why?


Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
if(b1.equals(b2))
System.out.println("We are Equal");

Equal.

98. To what values are local variables initialized?


False for Boolean, 0 for numeric, null for all other reference types.

Você também pode gostar