20 April 2011

interview questions: Inheritance in JAVA

Topic : Inheritance in JAVA

Faculty Name:Saritha G Pillai

(Q)  Can Abstract Class have constructors?

(Ans) Abstract class's can have a constructor, but you cannot access it through the object, since you cannot instantiate abstract class. To access the constructor create a sub class and extend the abstract class which is having the constructor.

Example
public abstract class AbstractExample {
public AbstractExample(){
System.out.println("In AbstractExample()");
}
}

public class Test extends AbstractExample{
public static void main(String args[]){
Test obj=new Test();
}
}

 http://www.roseindia.net/java/language/inheritance.shtml

http://www.tutorialspoint.com/java/java_inheritance.htm

http://www.javabeginner.com/learn-java/java-inheritance


 (Q) 

If interface & abstract class have same methods and those methods contain no implementation, which one would you prefer?

(Ans)Obviously one should ideally go for an interface, as we can only extend one class. Implementing an interface for a class is very much effective rather than extending an abstract class because we can extend some other useful class for this subclass

 http://www.roseindia.net/java/language/inheritance.shtml

http://www.tutorialspoint.com/java/java_inheritance.html

http://www.javabeginner.com/learn-java/java-inheritance

 (Q) What's the difference between an interface and an abstract class?
(Ans) Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Interface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods
Abstract class is a Class prefix with a abstract keyword followed by Class definition. Interface is a Interface which starts with interface keyword.
Abstract class contains one or more abstract methods. where as Interface contains all abstract methods and final declarations
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented.


http://www.roseindia.net/java/language/inheritance.shtml

http://www.tutorialspoint.com/java/java_inheritance.htm

http://www.javabeginner.com/learn-java/java-inheritance



Faculty Name:Arun Pallisseri



Q)Why is multiple inheritance not possible in Java?

Ans:)It depends on how you understand "inheritance". Java can only "extends" one super class, but can "implements" many interfaces; that doesn't mean the multiple inheritance is not possible. You may use interfaces to make inheritance work for you. Or you may need to work around. For example, if you cannot get a feature from a class because your class has a super class already, you may get that class's feature by declaring it as a member field or getting an instance of that class. So the answer is that multiple inheritance in Java is possible.



http://www.google.com/url?sa=t&source=web&cd=2&sqi=2&ved=0CCAQFjAB&url=http%3A%2F%2Fwww.fluffycat.com%2FJava%2F&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNE67JbqZwOj6x8nR2Na0d3KuM2VGw



Q)This class (IncrementImpl) will be used by various threads concurrently; can you see the inherent flaw(s)? How would you improve it?

Ans:)

public class IncrementImpl {

    private static int counter = 0;

    public synchronized void increment() {

        counter++;

    }

    public int getCounter() {

        return counter;

    }

}

http://www.google.com/url?sa=t&source=web&cd=4&sqi=2&ved=0CC4QFjAD&url=http%3A%2F%2Fchortle.ccsu.edu%2Fjava5%2Fcs151java.html&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNHAoXUyh6TXjer_yOGkXys4TwjVmQ



Q)The counter is static variable which is shared by multiple instances of this class. The increment() method is synchronized, but the getCounter() should be synchronized too. Otherwise the Java run-time system will not guarantee the data integrity and the race conditions will occur. The famous producer/consumer example listed at Sun's thread tutorial site will tell more.
one of solutions

public class IncrementImpl {

    private static int counter = 0;

    public synchronized void increment() {

        counter++;

    }

    public synchronized int getCounter() {

        return counter;

    }

}

http://www.google.com/url?sa=t&source=web&cd=2&sqi=2&ved=0CCAQFjAB&url=http%3A%2F%2Fwww.fluffycat.com%2FJava%2F&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNE67JbqZwOj6x8nR2Na0d3KuM2VGw





Q)What are the drawbacks of inheritance?

Since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation. In addition, the inheritance may make peers hardly understand your code if they don't know how your super-class acts and add learning curve to the process of development.
Usually, when you want to use a functionality of a class, you may use subclass to inherit such function or use an instance of this class in your class. Which is better, depends on your specification.

http://www.google.com/url?sa=t&source=web&cd=4&sqi=2&ved=0CC4QFjAD&url=http%3A%2F%2Fchortle.ccsu.edu%2Fjava5%2Fcs151java.html&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNHAoXUyh6TXjer_yOGkXys4TwjVmQ



 Q)Is there any other way that you can achieve inheritance in Java?

There are a couple of ways. As you know, the straight way is to "extends" and/or "implements". The other way is to get an instance of the class to achieve the inheritance. That means to make the supposed-super-class be a field member. When you use an instance of the class, actually you get every function available from this class, but you may lose the dynamic features of OOP



http://www.google.com/url?sa=t&source=web&cd=2&sqi=2&ved=0CCAQFjAB&url=http%3A%2F%2Fwww.fluffycat.com%2FJava%2F&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNE67JbqZwOj6x8nR2Na0d3KuM2VGw





 Q)Can a private method of a superclass be declared within a subclass?

Sure. A private field or method or inner class belongs to its declared class and hides from its subclasses. There is no way for private stuff to have a runtime overloading or overriding (polymorphism) features.

http://www.google.com/url?sa=t&source=web&cd=4&sqi=2&ved=0CC4QFjAD&url=http%3A%2F%2Fchortle.ccsu.edu%2Fjava5%2Fcs151java.html&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNHAoXUyh6TXjer_yOGkXys4TwjVmQ



 Q)What is the difference between superclass & subclass?

Ans : A super class is a class that is inherited whereas subclass is a class that does the inheriting.

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Which keyword is used to inherit a class?

Ans : extends

http://www.google.com/url?sa=t&source=web&cd=2&sqi=2&ved=0CCAQFjAB&url=http%3A%2F%2Fwww.fluffycat.com%2FJava%2F&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNE67JbqZwOj6x8nR2Na0d3KuM2VGw



 Q)When can subclasses not access superclass members?

Ans : When superclass is declared as private.

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Q)Which class does begin Java class hierarchy?

Ans : Object class

http://www.google.com/url?sa=t&source=web&cd=4&sqi=2&ved=0CC4QFjAD&url=http%3A%2F%2Fchortle.ccsu.edu%2Fjava5%2Fcs151java.html&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNHAoXUyh6TXjer_yOGkXys4TwjVmQ



 Q)What is inheritance?

Ans : Deriving an object from an existing class. In the other words, Inheritance is the process of inheriting all the features from a class

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



Q) What are the advantages of inheritance?

Ans : Reusability of code and accessibility of variables and methods of the superclass by subclasses.

http://www.google.com/url?sa=t&source=web&cd=2&sqi=2&ved=0CCAQFjAB&url=http%3A%2F%2Fwww.fluffycat.com%2FJava%2F&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNE67JbqZwOj6x8nR2Na0d3KuM2VGw



 Q)Which method is used to call the constructors of the superclass from the subclass?

Ans : super(argument)

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Q)Which is used to execute any method of the superclass from the subclass?

Ans : super.method-name(arguments)

http://www.google.com/url?sa=t&source=web&cd=4&sqi=2&ved=0CC4QFjAD&url=http%3A%2F%2Fchortle.ccsu.edu%2Fjava5%2Fcs151java.html&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNHAoXUyh6TXjer_yOGkXys4TwjVmQ

 Q)Which methods are used to destroy the objects created by the constructor methods?

Ans : finalize()

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Q)What are abstract classes?

Ans : Abstract classes are those for which instances can’t be created.

http://www.google.com/url?sa=t&source=web&cd=2&sqi=2&ved=0CCAQFjAB&url=http%3A%2F%2Fwww.fluffycat.com%2FJava%2F&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNE67JbqZwOj6x8nR2Na0d3KuM2VGw



 Q)What must a class do to implement an interface?

Ans: It must provide all of the methods in the interface and identify the interface in its implements clause.

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Which methods in the Object class are declared as final?

Ans : getClass(), notify(), notifyAll(), and wait()

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Q)Can we declare variable inside a method as final variables? Why?

Ans : Cannot because, local variable cannot be declared as final variables.

http://www.google.com/url?sa=t&source=web&cd=4&sqi=2&ved=0CC4QFjAD&url=http%3A%2F%2Fchortle.ccsu.edu%2Fjava5%2Fcs151java.html&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNHAoXUyh6TXjer_yOGkXys4TwjVmQ



Q) Can an abstract class may be final?

Ans : An abstract class may not be declared as final.

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Q)Does a class inherit the constructors of it's super class?

Ans: A class does not inherit constructors from any of it's super classes.

http://www.google.com/url?sa=t&source=web&cd=2&sqi=2&ved=0CCAQFjAB&url=http%3A%2F%2Fwww.fluffycat.com%2FJava%2F&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNE67JbqZwOj6x8nR2Na0d3KuM2VGw



 Q)What restrictions are placed on method overloading?

Ans: Two methods may not have the same name and argument list but different return types.

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Q)What restrictions are placed on method overriding?

Ans : Overridden methods must have the same name , argument list , and return type. The overriding method may not limit the access of the method it overridees.The overriding method may not throw any exceptions that may not be thrown by the overridden method.

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBYQFjAA&url=http%3A%2F%2Fwww.learn-java-tutorial.com%2FJava-Inheritance.cfm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNHMMGpBJhYWx3LaOPvZDiLAeO4Tog



 Q)What modifiers may be used with an inner class that is a member of an outer class?

Ans : a (non-local) inner class may be declared as public, protected, private, static, final or abstract.

http://www.google.com/url?sa=t&source=web&cd=4&sqi=2&ved=0CC4QFjAD&url=http%3A%2F%2Fchortle.ccsu.edu%2Fjava5%2Fcs151java.html&ei=g_iiTYa3IcG3cavUrOUB&usg=AFQjCNHAoXUyh6TXjer_yOGkXys4TwjVmQ



 Q)How this() is used with constructors?

Ans: this() is used to invoke a constructor of the same class

http://www.google.com/url?sa=t&source=web&cd=9&ved=0CFgQFjAI&url=http%3A%2F%2Fhome.cogeco.ca%2F~ve3ll%2Fjatutor5.htm&ei=wPiiTZalF4L5cb-ejMIH&usg=AFQjCNGRzWNCCofTPrAg7kQp0Q2HARZ67w



PHP blog questions





Faculty Name: Saritha G Pillai





Topic: Database Connection in PHP


(Q) what is differenc between mysql_connect and mysql_pconnect?

(Ans) mysql_connect opens up a database connection every time a page is loaded. mysql_pconnect opens up a connection, and keeps it open across multiple requests.

mysql_pconnect uses less resources, because it does not need to establish a database connection every time a page is loaded.



http://www.novaksblog.com/2007/10/02/database-connection-with-php/

http://www.webcheatsheet.com/PHP/connect_mysql_database.php


(Q)How To Get MySQL Statement Execution Errors?


(Ans)When you execute a MySQL statement with mysql_query(), and the statement failed, mysql_query() will return the Boolean value FALSE. This is good enough to tell that there is something wrong with that statement. But if you want to know more about the cause of the failure, you can use mysql_errno() and mysql_error() get the error number and error message. The tutorial exercise below shows you an improved version of the previous PHP script:

<?php
 $con = mysql_connect('localhost:8888', 'guest', 'pub');
 $sql = 'CREATE DATABASE fyicenter';
 if (mysql_query($sql, $con)) {
   print("Database fyicenter created.\n");
 } else {
   print("Database creation failed with error:\n");
   print(mysql_errno($con).": ".mysql_error($con)."\n");
 }
 mysql_close($con);
?>

If you run this script, you will get something like this:

Database creation failed with error:
1044: Access denied for user 'gues'@'%' to database 'fyicenter'



http://www.webcheatsheet.com/PHP/connect_mysql_database.php

http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/connect-to-mysql-database.aspx


Faculty Name:Viji.V



What is the difference between mysql_fetch_object and mysql_fetch_array?
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array

http://php.about.com/od/phpwithmysql/qt/mysql_connect.htm

http://dev.mysql.com/downloads/connector/php-mysqlnd/

http://php.net/manual/en/function.mysql-connect.php

http://www.w3schools.com/PHP/php_mysql_connect.asp





What are the different tables present in MySQL? Which type of table is generated when we are creating a table in the following syntax: create table employee(eno int(2),ename varchar(10))?
Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
MyISAM is the default storage engine as of MySQL 3.23. When you fire the above create query MySQL will create a MyISAM table.

http://php.about.com/od/phpwithmysql/qt/mysql_connect.htm

http://dev.mysql.com/downloads/connector/php-mysqlnd/

http://php.net/manual/en/function.mysql-connect.php

http://www.w3schools.com/PHP/php_mysql_connect.asp





How To Create a Table?
If you want to create a table, you can run the CREATE TABLE statement as shown in the following sample script:

<?php
include "mysql_connection.php";

$sql = "CREATE TABLE Tech_links ("
. " id INTEGER NOT NULL"
. ", url VARCHAR(80) NOT NULL"
. ", notes VARCHAR(1024)"
. ", counts INTEGER"
. ", time TIMESTAMP DEFAULT sysdate()"
. ")";
if (mysql_query($sql, $con)) {
print("Table Tech_links created.\n");
} else {
print("Table creation failed.\n");
}

mysql_close($con);
?>

Remember that mysql_query() returns TRUE/FALSE on CREATE statements. If you run this script, you will get something like this:
Table Tech_links created.

http://www.tizag.com/mysqlTutorial/mysqlconnection.php

http://www.sqlstrings.com/mysql-php-conection.htm

http://www.siteground.com/tutorials/php-mysql/php_mysql_connection.htm



How can we create a database using PHP and mysql?
We can create MySQL database with the use of mysql_create_db($databaseName) to create a database.

http://www.tizag.com/mysqlTutorial/mysqlconnection.php

http://www.sqlstrings.com/mysql-php-conection.htm

http://www.siteground.com/tutorials/php-mysql/php_mysql_connection.htm



How many ways we can retrieve the date in result set of mysql using php?
As individual objects so single record or as a set or arrays.

What is the maximum length of a table name, a database name, or a field name in MySQL?
Database name: 64 characters
Table name: 64 characters
Column name: 64 characters

http://www.tizag.com/mysqlTutorial/mysqlconnection.php

http://www.sqlstrings.com/mysql-php-conection.htm

http://www.siteground.com/tutorials/php-mysql/php_mysql_connection.htm



How many values can the SET function of MySQL take?
MySQL SET function can take zero or more values, but at the maximum it can take 64 values.

http://www.tizag.com/mysqlTutorial/mysqlconnection.php

http://www.sqlstrings.com/mysql-php-conection.htm

http://www.siteground.com/tutorials/php-mysql/php_mysql_connection.htm



What are the differences between  mysql_fetch_array(),  mysql_fetch_object(),  mysql_fetch_row()?
Answer 1:
mysql_fetch_array() -> Fetch a result row as a combination of associative array and regular array.
mysql_fetch_object() -> Fetch a result row as an object.
mysql_fetch_row() -> Fetch a result set as a regular array().

Answer 2:
The difference between mysql_fetch_row() and mysql_fetch_array() is that the first returns the results in a numeric array ($row[0], $row[1], etc.), while the latter returns a the results an array containing both numeric and associative keys ($row['name'], $row['email'], etc.). mysql_fetch_object() returns an object ($row->name, $row->email, etc.).

http://php.about.com/od/phpwithmysql/qt/mysql_connect.htm

http://dev.mysql.com/downloads/connector/php-mysqlnd/

http://php.net/manual/en/function.mysql-connect.php

http://www.w3schools.com/PHP/php_mysql_connect.asp





What are the MySQL database files stored in system ?
Data is stored in name.myd
Table structure is stored in name.frm
Index is stored in name.myi

http://www.tizag.com/mysqlTutorial/mysqlconnection.php

http://www.sqlstrings.com/mysql-php-conection.htm

http://www.siteground.com/tutorials/php-mysql/php_mysql_connection.htm



Who is the father of PHP and what is the current version of PHP and MYSQL?
Rasmus Lerdorf.
PHP 5.1. Beta
MySQL 5.0

http://www.tizag.com/mysqlTutorial/mysqlconnection.php

http://www.sqlstrings.com/mysql-php-conection.htm

http://www.siteground.com/tutorials/php-mysql/php_mysql_connection.htm



In how many ways we can retrieve data in the result set of MYSQL using PHP?
mysql_fetch_array - Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_assoc - Fetch a result row as an associative array
mysql_fetch_object - Fetch a result row as an object
mysql_fetch_row —- Get a result row as an enumerated array

http://php.about.com/od/phpwithmysql/qt/mysql_connect.htm

http://dev.mysql.com/downloads/connector/php-mysqlnd/

http://php.net/manual/en/function.mysql-connect.php
http://www.w3schools.com/PHP/php_mysql_connect.asp


For the latest IT job openings visit www.ipsrjobs.com

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete