1) Strings

searchcoilΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 5 μέρες)

265 εμφανίσεις


Rekha Sebastian


1. How do

compare two Strings? Why cannot we use == to compare two String literals?

Ans. To compare two String literals, you should use equals method. The equals method of
java.lang.Object has b
een overridden by the String class to return true if the two strings have the
same length and exactly same Unicode characters.

For example:

String s1 = new String(“Java”);

String s2 = new String(“Java”);

if ( s1.equals(s2) ) {

strings s1 and s2 are equal.");


else {

System.out.println("The strings s1 and s2 are not equal.");


The output of the above will be "The strings s1 and s2 are equal."

And when we use == operator of the java.lang.Object class which tests whether two
objects are
same instead of equals method as below

String s1 = new String(“Java”);

String s2 = new String(“Java”);

if ( s1 == s2 ) {

System.out.println("The strings s1 and s2 are equal.");


else {

System.out.println("The strings s1 and s2 are not eq


The output will be "The strings s1 and s2 are not equal."

However, when String literals are considered for comparison as below

String s1 = “Java”;

String s2 = “Java”;

if ( s1 == s2 ) {

System.out.println("The strings s1 and s2 are equal.")


else {

System.out.println("The strings s1 and s2 are not equal.");


The output will be "The strings s1 and s2 are equal.". This is because compiler optimizes the two
string literals as one String object. And hence == operator returns true.

ing Types

What is upcasting / downcasting? How instanceOf operator can be used to avoid

Suppose we have the following class hierarchy


Line Rectangle

Shape s1 ;

S1 = new Line();

The casting done in the line abov
e from a more extended class to a less extended class is called
upcasting or widening.

Suppose we have

Line l1;

l1 =(Line) s1;

Passing the shape reference when an object of type line is expected, the casting referred to as
downcasting or casting is do
ne down the class hierarchy using an explicit cast. The above
statement would have given ClassCastException if s1 doesn’t actuall;y refers to line object. In the
example class hierarchy considered above it can also refer to Rectangle object. So if the s1 w
actually referring to Rectangle object, the casting l1 =(Line) s1; is not correct.

This can be handled using extended functionality in two different ways as follows


Simply do the cast and handle the exception.


Use instanceOf operator

Public void dr
aw(Shape s1){

If ( s1 instanceOf Line){

l1 =(Line) s1;

use line functionalities



Using instanceOf operator may be a better approach because exception handling may
consume more time.

Variable Scope

Does the following code compile?

Public class Test{


int x = 1;



public static void main( String[] args){



Ans. This will give compilation error because the scope of variable x is only in the block.
But if x was a member variable, it w
ould have compiled.

Java inheritance.

Does the java supports multiple Inheritance? How is multiple Inheritance done in Java?

Java programming language does not support Multiple Inheritance or a class cannot
extend more than one superc
lass. Java uses single inheritance model. Extending a class
inherits not only its super class’s contract but also its implementation

In Java, Multiple Inheritance is achieved using Interfaces by declaring Interfaces for
classes you need to inherit and im
plementing these Interfaces.

For a given class, the classes that are extended and the interfaces that are implemented
are called super types. That is a reference to the new class(sub type) is reference to any
of its super types

5. Overriding Methods:

s the following Code Snippet Compile? Why?

Class Shape{

public void draw() throws MyException{

System.out.println(" I am in shape");



public class Rectangle extends Shape{

protected void draw() throws Exception {

System.out.println(" I am in Rectangl



class MyException extends Exception{}

Ans. It gives compilation errors due to 2 reasons.


One because of the throws clause of the overriding method throws super type of the
Exception that thrown in the super class. The over riding method

s throws

should be either same or subtype of the exceptions listed in the super classes’s


The second reason is because, the access specifier of the subclass

has less access
than the super class method
. An extended class may change the access of the

class, but only if it provides more access.

6. Junit

What is the use of test fixture in Junit test?

The test fixtures are useful when you are testing multiple methods on a common object
as shown in the example below. The test fixture setup() and

teardown can be used to
initialize and clean up of the object respectively because setup method is called before
every test case method and teardown method is invoked after every test case.
Implementation of test fixtures avoids duplication of the code f
or initialization and

import junit.framework.TestCase;

public class TestArrayQueue extends TestCase {

ArrayQueue q = new ArrayQueue ();.

// Sets up the test fixture.

protected void setUp() {

//initialization code

q = new ArrayQueue ();.


//Tears down the test fixture.

protected void tearDown() {

// release objects under test here



public void TestIsEmpty(){

assert (q.isEmpty());


public v
oid TestSize(){




` }

7 Your IDE (Eclipse, Net Beans, etc.)

List some of the
core features

of Netbeans

IDE that help reduce software development life

The Netbeans
features like abbreviations, c
ode completion, and word matching largely
reduce the
coding time

and edit errors

b. Coding using Netbeans is Simple and easy

c. The project structure is well organised

Support to most latest of the java language features

. Supports testing using Juni
t test

Supports J2EE development

such as creating web services

g.Project based on Apache Ant can be developed on this IDE. This feature enables deployment
of project even without

8 Java debugging

Explain some of the best practices employed during cod
e development that may support code
debugging as well as unit and integration testing?

Include Assertions into programs. Assertions including pre
condition and post
help to detect errors that may be hard to notice at various points in the progr
Assertions are Boolean expressions, which must be true at the points in code, in which
they are inserted. Any place where an assertion fails indicates that the system/code is in
an inconsistent state at that point. Assertions go a long way in determini
ng and localizing
the bug during the development, testing and debugging stages. Parts of code segment
with no assertion violations indicate that the program fully implements the contracted
logic and can be easily integrated with other parts of the programs

Place conditional debug segments in the code, that can be turned off or on, based on
flagging condition during compilation or run
time, to enable debugging. Code segments
that can be enabled during compile
time offer very little or zero overhead to the

performance of the code. Examples are #ifdef statements. However, runtime debug
switches offer maximum debug flexibility, by being able to test the code, without any re

9 Java interfaces

Explain the different type of design feature
s offered by Java Interfaces


Interface he
lp declare functional behaviors.

For example Runnable Interface declares run() method. This run() method supports the
Thread functionality


Classify objects based on a purpose for which it may be used.

The interf
aces used for this purpose does not contain any methods. An example for such
interface is java.io.
Serializable. Eventhough the interface does not require any method to
be implemented, it indicates that the object will be serialized in applications

va.rmi.Remote interface declares no methods. Its sole purpose is to indicate that an
object is a remote object


As a way of providing multiple inheritance.

The multiple inheritance is achieved in java using the interfaces. Declare the
with the abstract methods for the
classes you need to inherit and implementing
these Interfaces.

10 Certificates

How do

support Application security

Applications launched using Java across the
, run in a restricted environment
where they
have limited access to local computing resources. In order for securely running the java code
over the network, java uses digital signing of certificates. If the verification fails, the java will not
run the respective application. This service
makes it possible for users to verify that an application
comes from a trusted source. Because the application service provider signs the code, both user
and service provider can be ensured that no other party can impersonate the application on the
Web. A
signed application that is trusted by the user can also request additional system
privileges, such as access to a local disk.