What Is The Difference Between Private, Protected, and Public?
What Is The Difference Between Private, Protected, and Public?
These keywords are for allowing privileges to components such as java methods
and variables.
Public: accessible to all classes
Private: accessible only to the class to which they belong
Protected: accessible to the class to which they belong and any subclasses.
Access specifiers are keywords that determines the type of access to the member
of a class. These are:
* Public
* Protected
* Private
* Defaults
Similarities:
* Neither Abstract classes or Interface can be instantiated.
Garbage collection is one of the most important feature of Java, Garbage collection
is also called automatic memory management as JVM automatically removes the
unused variables/objects (value is null) from the memory. User program can't
directly free the object from memory, instead it is the job of the garbage collector
to automatically free the objects that are no longer referenced by a program.
Every class inherits finalize() method from java.lang.Object, the finalize() method
is called by garbage collector when it determines no more references to the object
exists. In Java, it is good idea to explicitly assign null into a variable when no
more in use. I Java on calling System.gc() and Runtime.gc(), JVM tries to recycle
the unused objects, but there is no guarantee when all the objects will garbage
collected.
5. Can you call one constructor from another if a class has multiple
constructors
Yes. Use this() to call a constructor from an other constructor.
7. Explain in your own words the "bottom line" benefits of the use of an
interface.
The interface makes it possible for a method in one class to invoke methods on
objects of other classes, without the requirement to know the true class of those
objects, provided that those objects are all instantiated from classes that
implement one or more specified interfaces. In other words, objects of classes
that implement specified interfaces can be passed into methods of other objects
as the generic type Object, and the methods of the other objects can invoke
methods on the incoming objects by first casting them as the interface type.
10. What's the difference between the methods sleep() and wait()
The code sleep(1000); puts thread aside for exactly one second. The code
wait(1000), causes a wait of up to one second. A thread could stop waiting earlier
if it receives the notify() or notifyAll() call. The method wait() is defined in the
class Object and the method sleep() is defined in the class Thread.
11. What would you use to compare two String variables - the operator == or
the method equals()?
I'd use the method equals() to compare the values of the Strings and the == to
check if two variables point at the same instance of a String object.
12. Why would you use a synchronized block vs. synchronized method?
Synchronized blocks place locks for shorter periods than synchronized methods.
13. What access level do you need to specify in the class declaration to ensure
that only classes from the same directory can access it?
You do not need to specify any access level, and Java will use a default package
access level.
14. Can an inner class declared inside of a method access local variables of this
method?
It's possible if these variables are final.
15. What can go wrong if you replace && with & in the following code:
String a=null; if (a!=null && a.length()>10) {...}
A single ampersand here would lead to a NullPointerException.
Following table lists the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void
18. How could Java classes direct program messages to the system console, but
error messages, say to a file?
The class System has a variable out that represents the standard output, and the
variable err that represents the standard error device. By default, they both point
at the system console. This how the standard output could be re-directed:
Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st);
System.setOut(st);
20. When you assign a subclass to a variable having a supeclass type, the
casting is performed automatically. Can you write a Java class that could be
used both as an applet as well as an application?
Yes. Add a main() method to the applet.
22. What's the difference between J2SDK 1.5 and J2SDK 5.0?
There's no difference, Sun Microsystems just re-branded this version.
24. Name the containers which uses Border Layout as their default layout?
Containers which uses Border Layout as their default are: window, Frame and
Dialog classes.
25. You are planning to do an indexed search in a list of objects. Which of the
two Java collections should you use:
ArrayList or LinkedList?
ArrayList
30. You can create an abstract class that contains only abstract methods. On the
other hand, you can create an interface that declares the same methods. So can
you use abstract classes instead of interfaces?
Sometimes. But your class may be a descendent of another class and in this case
the interface is your only option.
31. If you're overriding the method equals() of an object, which other method
you might also consider?
hashCode()
33. How would you make a copy of an entire Java object with its state?
Have this class implement Cloneable interface and call its method clone().
34. How can you minimize the need of garbage collection and make the
memory use more effective?
Use object pooling and weak object references.
35. There are two classes: A and B. The class B need to inform a class A when
some important event has happened. What Java technique would you use to
implement it?
If these classes are threads I'd consider notify() or notifyAll(). For regular classes
you can use the Observer interface.
* Method overloading
* Method overriding through inheritance
* Method overriding through the Java interface
3. What are user defined datatypes and when you should go for them?
User defined datatypes let you extend the base SQL Server datatypes by
providing a descriptive name, and format to the database. Take for example, in
your database, there is a column called Flight_Num which appears in many
tables. In all these tables it should be varchar(8). In this case you could create a
user defined datatype called Flight_num_type of varchar(8) and use it across all
your tables.
4. What is bit datatype and what's the information that can be stored
inside a bit column?
Bit datatype is used to store boolean information like 1 or 0 (true or false). Untill
SQL Server 6.5 bit datatype could hold either a 1 or 0 and there was no support
for NULL. But from SQL Server 7.0 onwards, bit datatype can represent a third
state, which is NULL.
14. What's the difference between DELETE TABLE and TRUNCATE TABLE
commands?
DELETE TABLE is a logged operation, so the deletion of each row gets logged in
the transaction log, which makes it slow. TRUNCATE TABLE also deletes all the
rows in a table, but it won't log the deletion of each row, instead it logs the
deallocation of the data pages of the table, which makes it faster. Of course,
TRUNCATE TABLE can be rolled back.
16. What are the new features introduced in SQL Server 2000 (or the latest
release of SQL Server at the time of your interview)? What changed between
the previous version of SQL Server and the current version?
This question is generally asked to see how current is your knowledge. Generally
there is a section in the beginning of the books online titled "What's New", which
has all such information. Of course, reading just that is not enough, you should
have tried those things to better answer the questions. Also check out the section
titled "Backward Compatibility" in books online which talks about the changes
that have taken place in the new version.
For an explanation of these constraints see books online for the pages titled:
"Constraints" and "CREATE TABLE", "ALTER TABLE"
2. What is an index? What are the types of indexes? How many clustered
indexes can be created on a table? I create a separate index on each column of a
table. what are the advantages and disadvantages of this approach?
Indexes in SQL Server are similar to the indexes in books. They help SQL Server
retrieve the data quicker.
Indexes are of two types. Clustered indexes and non-clustered indexes. When
you craete a clustered index on a table, all the rows in the table are stored in the
order of the clustered index key. So, there can be only one clustered index per
table. Non-clustered indexes have their own storage separate from the table data
storage. Non-clustered indexes are stored as B-tree structures (so do clustered
indexes), with the leaf level nodes having the index key and it's row locater. The
row located could be the RID or the Clustered index key, depending up on the
absence or presence of clustered index on the table.
4. What are the steps you will take to improve performance of a poor
performing query?
This is a very open ended question and there could be a lot of reasons behind the
poor performance of a query. But some general issues that you could talk about
would be: No indexes, table scans, missing or out of date statistics, blocking,
excess recompilations of stored procedures, procedures and triggers without SET
NOCOUNT ON, poorly written query with unnecessarily complicated joins, too
much normalization, excess usage of cursors and temporary tables.
Download the white paper on performance tuning SQL Server from Microsoft
web site. Don't forget to check out sql-server-performance.com
5. What are the steps you will take, if you are tasked with securing an SQL
Server?
Again this is another open ended question. Here are some things you could talk
about: Preferring NT authentication, using server, database and application roles
to control access to the data, securing the physical database files using NTFS
permissions, using an unguessable SA password, restricting physical access to
the SQL Server, renaming the Administrator account on the SQL Server
computer, disabling the Guest account, enabling auditing, using multiprotocol
encryption, setting up SSL, setting up firewalls, isolating SQL Server from the
web server etc.
6. What is a deadlock and what is a live lock? How will you go about resolving
deadlocks?
Deadlock is a situation when two processes, each having a lock on one piece of
data, attempt to acquire a lock on the other's piece. Each process would wait
indefinitely for the other to release the lock, unless one of the user processes is
terminated. SQL Server detects deadlocks and terminates one user's process.
9. How to restart SQL Server in single user mode? How to start SQL Server in
minimal configuration mode?
SQL Server can be started from command line, using the SQLSERVR.EXE. This
EXE has some very important parameters with which a DBA should be familiar
with. -m is used for starting SQL Server in single user mode and -f is used to start
the SQL Server in minimal configuration mode. Check out SQL Server books
online for more parameters and their explanations.
10. As a part of your job, what are the DBCC commands that you commonly
use for database maintenance?
DBCC CHECKDB, DBCC CHECKTABLE, DBCC CHECKCATALOG, DBCC
CHECKALLOC, DBCC SHOWCONTIG, DBCC SHRINKDATABASE, DBCC
SHRINKFILE etc. But there are a whole load of DBCC commands which are very
useful for DBAs. Check out SQL Server books online for more information.
11. What are statistics, under what circumstances they go out of date, how do
you update them?
Statistics determine the selectivity of the indexes. If an indexed column has
unique values then the selectivity of that index is more, as opposed to an index
with non-unique values. Query optimizer uses these indexes in determining
whether to choose an index or not while executing a query.
12. What are the different ways of moving data/databases between servers and
databases in SQL Server?
There are lots of options available, you have to choose your option depending
upon your requirements. Some of the options you have are:
BACKUP/RESTORE, dettaching and attaching databases, replication, DTS, BCP,
logshipping, INSERT...SELECT, SELECT...INTO, creating INSERT scripts to
generate data.
14. What is database replication? What are the different types of replication
you can set up in SQL Server?
Replication is the process of copying/moving data between databases on the
same or different servers. SQL Server supports the following types of replication
scenarios:
o Snapshot replication
o Merge replication
See SQL Server books online for indepth coverage on replication. Be prepared to
explain how different replication agents function, what are the main system
tables used in replication etc.
15. How to determine the service pack currently installed on SQL Server?
The global variable @@Version stores the build number of the sqlservr.exe,
which is used to determine the service pack installed. To know more about this
process visit
What are cursors? Explain different types of cursors. What are the disadvantages
of cursors? How can you avoid cursors?
Cursors allow row-by-row processing of the resultsets.
Disadvantages of cursors: Each time you fetch a row from the cursor, it results in
a network roundtrip, where as a normal SELECT query makes only one
rowundtrip, however large the resultset is. Cursors are also costly because they
require more resources and temporary storage (results in more IO operations).
Furthere, there are restrictions on the SELECT statements that can be used with
some types of cursors.
Most of the times, set based operations can be used instead of cursors. Here is an
example:
If you have to give a flat hike to your employees using the following criteria:
Another situation in which developers tend to use cursors: You need to call a
stored procedure when a column in a particular row meets certain condition.
You don't have to use cursors for this. This can be achieved using WHILE loop,
as long as there is a unique key to identify each row.
17. Write down the general syntax for a SELECT statements covering all the
options
Here's the basic syntax: (Also checkout SELECT in books online for advanced
syntax).
SELECT select_list
[INTO new_table_]
FROM table_source
[WHERE search_condition]
[GROUP BY group_by_expression]
[HAVING search_condition]
[ORDER BY order_expression [ASC | DESC] ]
Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs.OUTER JOINs are
further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL
OUTER JOINS.
20. What is an extended stored procedure? Can you instantiate a COM object
by using T-SQL?
An extended stored procedure is a function within a DLL (written in a
programming language like C, C++ using Open Data Services (ODS) API) that
can be called from T-SQL,just the way we call normal stored procedures using
the EXEC statement. See books online to learn how to create extended stored
procedures and how to add them to SQL Server.
Yes, you can instantiate a COM (written in languages like VB, VC++) object from
T-SQL by using sp_OACreate stored procedure. Also see books online for
sp_OAMethod, sp_OAGetProperty, sp_OASetProperty, sp_OADestroy.
21. What is the system function to get the current user's user id?
USER_ID().Also check out other system functions like USER_NAME(),
SYSTEM_USER, SESSION_USER, CURRENT_USER, USER, SUSER_SID(),
HOST_NAME().
22. What are triggers? How many triggers you can have on a table? How to
invoke a trigger on demand?
Triggers are special kind of stored procedures that get executed automatically
when an INSERT, UPDATE or DELETE operation takes place on a table.
In SQL Server 6.5 you could define only 3 triggers per table, one for INSERT, one
for UPDATE and one for DELETE. From SQL Server 7.0 onwards, this restriction
is gone, and you could create multiple triggers per each action. But in 7.0 there's
no way to control the order in which the triggers fire. In SQL Server 2000 you
could specify which trigger fires first or fires last using sp_settriggerorder
Triggers are generally used to implement business rules, auditing. Triggers can
also be used to extend the referential integrity checks, but wherever possible, use
constraints for this purpose, instead of triggers, as constraints are much faster.
Till SQL Server 7.0, triggers fire only after the data modification operation
happens. So in a way, they are called post triggers. But in SQL Server 2000 you
could create pre triggers also. Search SQL Server 2000 books online for INSTEAD
OF triggers.
Java swings:
Java Swing is a GUI toolkit for Java. Swing is one part of the Java Foundation
Classes (JFC). Swing includes graphical user interface (GUI) widgets such as text
boxes, buttons, split-panes, and tables.
History
The Internet Foundation Classes (IFC) were a graphics library for Java originally
developed by Netscape Communications Corporation and first released on Dec
16, 1996.
Architecture
Swing favors relative layouts (which specify the positional relationships between
components), as opposed to absolute layouts (which specify the exact location
and size of components). The motivation for this is to allow Swing applications
to work and appear visually correct regardless of the underlying systems colors,
fonts, language, sizes or I/O devices. This can make screen design somewhat
difficult and numerous tools have been developed to allow visual designing of
screens.
Swing also uses a publish subscribe event model (as does AWT), where listeners
subscribe to events that are fired by the application (such as pressing a button,
entering text or clicking a checkbox). The model classes typically include, as part
of their interface, methods for attaching listeners (this is the publish aspect of the
event model).
The frequent use of loose coupling within the framework makes Swing
programming somewhat different from higher-level GUI design languages and
4GLs. This is a contributing factor to Swing having such a steep learning curve.
Swing allows one to specialize the look and feel of widgets, by modifying the
default (via runtime parameters), deriving from an existing one, by creating one
from scratch, or, beginning with J2SE 5.0, by using the skinnable Synth Look and
Feel, which is configured with an XML property file. The look and feel can be
changed at runtime, and early demonstrations of Swing would frequently
provide a way to do this.
Relationship to AWT
Since early versions of Java, a portion of the Abstract Windowing Toolkit (AWT)
has provided platform independent APIs for user interface components. In AWT,
each component is rendered and controlled by a native peer component specific
to the underlying windowing system.
Relationship to SWT
The advent of SWT has given rise to a great deal of division among Java desktop
developers with many strongly favouring either SWT or Swing. A renewed focus
on Swing look and feel fidelity with the native windowing toolkit in the
approaching Java SE 6 release (as of February 2006) is probably a direct result of
this.
Example
import javax.swing.JFrame;
import javax.swing.JLabel;
Applets
A Java applet is an applet delivered in the form of Java bytecode. Java applets
can run in a Web browser using a Java Virtual Machine (JVM), or in Sun's
AppletViewer, a stand alone tool to test applets. Java applets were introduced in
the first version of the Java language in 1995. Java applets are usually written in
the Java programming language but they can also be written in other languages
that compile to Java bytecode such as Jython.
Technical information
Java applets are executed in a sandbox by most web browsers, preventing them
from accessing local data. The code of the applet is downloaded from a web
server and the browser either embeds the applet into a web page or opens a new
window showing the applet's user interface. The applet can be displayed on the
web page by making use of the deprecated applet HTML element or the
recommended object element. This specifies the applet's source and the applet's
location statistics.
Advantages of applets
Disadvantages of applets
* it requires the Java plug-in, which isn't available by default on all web browsers
* it can't start up until the Java Virtual Machine is running, and this may have
significant startup time the first time it is used
* if it is uncached, it must be downloaded (usually over the internet), and this
takes time
* it is considered more difficult to build and design a good user interface with
applets than with HTML-based technologies
* if untrusted, it has severely limited access to the user's system - in particular
having no direct access to the client's disc or clipboard
* some organizations only allow software installed by the administrators. As a
result, many users cannot view applets by default.
* applets may require a specific JRE.
Compatibility issues
Alternatives
Alternative technologies exist (for example, DHTML and Flash) that satisfy some
of the scope of what is possible with an applet.
Another alternative to applets for client side Java is Java Web Start, which runs
outside the browser. In addition to the features available to applets, a simple
permissions box can give Java Web Start programs read and/or write access to
specified files stored on the client, and to the client's clipboard.