public class SpiralMatrix {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = new int[][]{ {1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
printSpiralMatrix(a,4,4);
String s = "sekhar";
s.substring(s.length());
s.substring(3, 3);
try{
String myString = "Indian";
//myString.substring(2,10);
}catch(Exception e){
e.printStackTrace();
}
}
private static void printSpiralMatrix(int[][] a, int m, int n) {
// m = total number of rows = 4
// n = total number of columns
int k = 0; // Row starts
int l = 0; // column starts
while(k<m && l<n){
//print first row....
for(int i = l;i<n;i++){
System.out.println(a[k][i]);
}
k++;
for(int i=k;i<m;i++){
System.out.println(a[i][n-1]);
}
n--;
for(int i= n-1; i>=l;--i){
System.out.println(a[m-1][i]);
}
m--;
for(int i=m-1;i>=k;--i){
System.out.println(a[i][l]);
}
l++;
}
}
}
Hi, This blog is the place who want to prepare for programming interview with major product development companies. Apart from interview questions I would like to provide some concepts on the new technologies which I learn as part of my carrier. Thanks for visiting my Blog.
Monday, October 17, 2011
printing spiral matrix in java
Monday, March 14, 2011
Wednesday, March 2, 2011
Print Ancestors of given Node in a Binary Tree
Question : Print Ancestors of given Node in a Binary Tree ?
Ans :
private static boolean printAncestors(Node root, int target) {
if(root == null)
return false;
else if(root.val == target){
return true;
}else if(printAncestors(root.left,target) || (printAncestors(root.right,target))){
System.out.println("Ancestor : "+root.val);
return true;
}else{
return false;
}
}
Question : Print Level Order traversal of Given BST ?
Ans: private static void printLevelOrderBST(Node node) {
if(queue.isEmpty()){
queue.add(node);
}
while(!queue.isEmpty()) {
Node myNode = queue.remove();
System.out.println(" Value : "+myNode.val);
if(myNode.left != null ){
queue.add(myNode.left);
}
if(myNode.right != null){
queue.add(node.right);
}
}
}
Ans :
private static boolean printAncestors(Node root, int target) {
if(root == null)
return false;
else if(root.val == target){
return true;
}else if(printAncestors(root.left,target) || (printAncestors(root.right,target))){
System.out.println("Ancestor : "+root.val);
return true;
}else{
return false;
}
}
Question : Print Level Order traversal of Given BST ?
Ans: private static void printLevelOrderBST(Node node) {
if(queue.isEmpty()){
queue.add(node);
}
while(!queue.isEmpty()) {
Node myNode = queue.remove();
System.out.println(" Value : "+myNode.val);
if(myNode.left != null ){
queue.add(myNode.left);
}
if(myNode.right != null){
queue.add(node.right);
}
}
}
creating Binary Tree in Java
Question : Write the code to Create the Binary Tree from given elements?
Ans : Example : 1,2,3,4,5,7
Binary Tree: 1
/ \
2 3
/ \
4 5
/
7
Here is the Code :
public class BinaryTreeConstruction {
public static void main(String[] args) {
/* Create root of the Binary Tree */
Node node = new Node(1);
if(root == null){
root = node;
}
int[] values = {2,3,4,5,7};
for(int val: values)
{
constructBinaryTree(node,val);
}
}
/* Construct Binary Tree */
private static void constructBinaryTree(Node node,int val)
Ans : Example : 1,2,3,4,5,7
Binary Tree: 1
/ \
2 3
/ \
4 5
/
7
Here is the Code :
public class BinaryTreeConstruction {
public static void main(String[] args) {
/* Create root of the Binary Tree */
Node node = new Node(1);
if(root == null){
root = node;
}
int[] values = {2,3,4,5,7};
for(int val: values)
{
constructBinaryTree(node,val);
}
}
/* Construct Binary Tree */
private static void constructBinaryTree(Node node,int val)
{
Node newNode = new Node(val);
if(node.left == null)
{
node.left = newNode;
}
else if(node.right == null)
{
node.right = newNode;
}
else if(node.left != null)
{
while(node.left != null && node.right != null)
{
node = node.left;
} if(node.left == null)
{
node.left = newNode;
}else if(node.right == null)
{
node.right = newNode;
}
}
}
}/* Class Close */
Class Node :
public class Node {
int val;
Node left,right;
Node(int value){
left = right = null;
this.val = value;
}
}
Class Node :
public class Node {
int val;
Node left,right;
Node(int value){
left = right = null;
this.val = value;
}
}
Monday, February 7, 2011
Some Switch loop concepts
Note 1 : In the Switch Expression only the variables it can be implicitly cast to 'Int'
can only be allowed. (or) the Java 5 feature enum's are allowed.
Note 2 : Even it does not support the other numeric types like long,float,double.
Note 3: So, it supports only char, byte, int, short.
can only be allowed. (or) the Java 5 feature enum's are allowed.
Note 2 : Even it does not support the other numeric types like long,float,double.
Note 3: So, it supports only char, byte, int, short.
Tuesday, January 25, 2011
Java JDBC Questions
Question : What is JDBC ?
Ans : Java Database Connectivity , Provide a java interface to Relational database.
Flow Pattern :
Application ----- > JDBC --------> Driver ------------>Database..
Question: How to get the Instance of Driver ?
Ans : Class Class.forName("name of the JDBC Driver");
Steps in Creating a Database Connection in JAva/
Step 1 : load the Driver.
--> through Class.forName("Name of the Driver");
This helps you to establish a connection between front end back-end of Application.
Step 2 : Creating a oracle JDBC Connection ?
DriverManger.getConnection();
Helps you to get the Connection Object,helps you to build connection between
URL and Database.
Step3 : Create JDBC statement Object.
Step4 : Execute SQL statements?
Question : What is DriverManager Class?
Ans: Which creates objects which can connect Java applications to database.
It manages the Drivers that are installed on the System.
getConnection() ,method uses to establish connection to the Database.
Syntax of getConnection(dbURL,userName,Password);
dbUrl == java:subProtocol:subName.
Each Driver has it's own subProtocol Syntax :
Example : for JDBC-ODBC Driver -> sun.jdbc.odbc.JdbcOdbcDriver
for MYSQL : mysql
Question : what is JDBC connection :
Ans: A jdbc connection is session/connection, within context of connection SQL statements
get Executed and results are returned.
A Connection Object provides MetaData, information about the database, fields,tables etc..
A Connection interface defines methods to interact with database.
Question : what is statement Object ?
Ans : A Statement Object is used to send and execute statements to Database.
Question : What are the different types of statements?
Ans : Statement : A simple Sql query Statement.
Prepared Statement : Pre-Compiled Sql Queries
Callable Statement : A call to Database Stored Procedure.
what is the Difference between DML , DDL , DCL commands /
Ans : DDL --- use to define the Structure/scheme of a Database.
Example : Alter, Create, Drop, truncate,Rename.
DML -- Use to manage the data within the scheme Objects
Example : UPDATE,SELECT , INSET, DELETE,MERGE, CALL
DCL : GRANT, REVOKE.
TCL[ Transaction Control ] : used to manage the changes made by the DML commands
example : COMMIT, SAVEPOINT,ROllback.SetRollback.
Question : What is the Difference between EXECUTE(), ExecuteQuery(), ExecuteUpdate()?
Ans :
boolean Execute() . == Will do both the Operations of ExecuteQuery and ExecuteUpdate.
ResultSet ExecuteQuery();
int ExecuteUpdate(); Insert,Update,Delete. or an SQL query which returns nothing.
What are the different Database Drivers available ?
Ans :
1) JDBC-ODBC Drivers.
2) Native - API partly Java Driver.
3) Net Protocol Java Driver.
4) Native Protocol Java Driver.
What are the Difference between these drivers?
Ans : Type 1 : Converts all JDBC calls into ODBC calls.
Type 4 : No Conversion requires purely java Driver.
Type 1 : Need to install third Party[ ODBC Drivers ] Drivers.
Type 4 : No Need.
Type 1 and 3 are Database independent .
Type 2 and 4 are database Dependent.
http://www.bestjavainterviewquestions.com/java-jdbc-questions/
Question : How to Debug problems related to JDBC ?
Ans : DriverManager.setLogWriter(PrintWriter out);
Question : What Causes the "no Suitable driver" error?
Ans : Specifying invalid URL in DriverManager.getConnection(); - Where the driver is
unable to recognize that URL.
Question : How to get the Number of rows in a ResultSet ?
Ans :
1. Invoke ResultSet.last() on the ResultSet.
2. Invoke ResultSet.getRow() to get the current row number (which is also the number of rows in the ResultSet)
3. Invoke ResultSet.beforeFirst() on the ResultSet.
4. Process as normal.
Ans : Java Database Connectivity , Provide a java interface to Relational database.
Flow Pattern :
Application ----- > JDBC --------> Driver ------------>Database..
Question: How to get the Instance of Driver ?
Ans : Class Class.forName("name of the JDBC Driver");
Steps in Creating a Database Connection in JAva/
Step 1 : load the Driver.
--> through Class.forName("Name of the Driver");
This helps you to establish a connection between front end back-end of Application.
Step 2 : Creating a oracle JDBC Connection ?
DriverManger.getConnection();
Helps you to get the Connection Object,helps you to build connection between
URL and Database.
Step3 : Create JDBC statement Object.
Step4 : Execute SQL statements?
Question : What is DriverManager Class?
Ans: Which creates objects which can connect Java applications to database.
It manages the Drivers that are installed on the System.
getConnection() ,method uses to establish connection to the Database.
Syntax of getConnection(dbURL,userName,Password);
dbUrl == java:subProtocol:subName.
Each Driver has it's own subProtocol Syntax :
Example : for JDBC-ODBC Driver -> sun.jdbc.odbc.JdbcOdbcDriver
for MYSQL : mysql
Question : what is JDBC connection :
Ans: A jdbc connection is session/connection, within context of connection SQL statements
get Executed and results are returned.
A Connection Object provides MetaData, information about the database, fields,tables etc..
A Connection interface defines methods to interact with database.
Question : what is statement Object ?
Ans : A Statement Object is used to send and execute statements to Database.
Question : What are the different types of statements?
Ans : Statement : A simple Sql query Statement.
Prepared Statement : Pre-Compiled Sql Queries
Callable Statement : A call to Database Stored Procedure.
what is the Difference between DML , DDL , DCL commands /
Ans : DDL --- use to define the Structure/scheme of a Database.
Example : Alter, Create, Drop, truncate,Rename.
DML -- Use to manage the data within the scheme Objects
Example : UPDATE,SELECT , INSET, DELETE,MERGE, CALL
DCL : GRANT, REVOKE.
TCL[ Transaction Control ] : used to manage the changes made by the DML commands
example : COMMIT, SAVEPOINT,ROllback.SetRollback.
Question : What is the Difference between EXECUTE(), ExecuteQuery(), ExecuteUpdate()?
Ans :
boolean Execute() . == Will do both the Operations of ExecuteQuery and ExecuteUpdate.
ResultSet ExecuteQuery();
int ExecuteUpdate(); Insert,Update,Delete. or an SQL query which returns nothing.
What are the different Database Drivers available ?
Ans :
1) JDBC-ODBC Drivers.
2) Native - API partly Java Driver.
3) Net Protocol Java Driver.
4) Native Protocol Java Driver.
What are the Difference between these drivers?
Ans : Type 1 : Converts all JDBC calls into ODBC calls.
Type 4 : No Conversion requires purely java Driver.
Type 1 : Need to install third Party[ ODBC Drivers ] Drivers.
Type 4 : No Need.
Type 1 and 3 are Database independent .
Type 2 and 4 are database Dependent.
http://www.bestjavainterviewquestions.com/java-jdbc-questions/
Question : How to Debug problems related to JDBC ?
Ans : DriverManager.setLogWriter(PrintWriter out);
Question : What Causes the "no Suitable driver" error?
Ans : Specifying invalid URL in DriverManager.getConnection(); - Where the driver is
unable to recognize that URL.
Question : How to get the Number of rows in a ResultSet ?
Ans :
1. Invoke ResultSet.last() on the ResultSet.
2. Invoke ResultSet.getRow() to get the current row number (which is also the number of rows in the ResultSet)
3. Invoke ResultSet.beforeFirst() on the ResultSet.
4. Process as normal.
Question :
Wednesday, December 29, 2010
Java Memory Management
Where local variables in Java Store?
Ans: Local variables store inside the Stack of JVM
All class instances and Arrays are allocates in Heap Memory.
Question : What is output of the below program?
Ans :
String s1 = "MY Name is Chakra";
String s2 = "MY Name is Chakra";
if(s2 == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
}else{
System.out.println(" s1 != s2 ");
}
if(s1.equals(s2)){
System.out.println(" s1 equals s2 ");
}else{
System.out.println(" s1 not equals s2");
}
o/p : s1 == s2
s1 equals s2
Question 2 :
String s1 = "MY Name";
s1 = s1 + " is Chakra";
String s2 = "MY Name is Chakra";
if(s1 == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
}else{
System.out.println(" s1 != s2 ");
}
o/p : s1 != s2
s1 equals s2
Question 3 :
String s1 = new String("MY Name is Chakra");
if(s1 == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
}else{
System.out.println(" s1 != s2 ");
}
o/p: s1 != s2
s1 equals s2
Question : What are the interfaces a string class implements?
Ans: Serializable , Comparable, charSequence.
Question : If your class overrides the hashCode method , how will you get the
int value of default hashCode() .
Ans : Use System.identityHashCode(obj1);
Question: how can you make your class immutable ?
Ans: Declare all variables as private, and do not provide setters and getters.
Ans: Local variables store inside the Stack of JVM
All class instances and Arrays are allocates in Heap Memory.
Question : What is output of the below program?
Ans :
String s1 = "MY Name is Chakra";
String s2 = "MY Name is Chakra";
if(s2 == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
}else{
System.out.println(" s1 != s2 ");
}
if(s1.equals(s2)){
System.out.println(" s1 equals s2 ");
}else{
System.out.println(" s1 not equals s2");
}
o/p : s1 == s2
s1 equals s2
Question 2 :
String s1 = "MY Name";
s1 = s1 + " is Chakra";
String s2 = "MY Name is Chakra";
if(s1 == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
}else{
System.out.println(" s1 != s2 ");
}
o/p : s1 != s2
s1 equals s2
Question 3 :
String s1 = new String("MY Name is Chakra");
if(s1 == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
}else{
System.out.println(" s1 != s2 ");
}
o/p: s1 != s2
s1 equals s2
Question : What are the interfaces a string class implements?
Ans: Serializable , Comparable, charSequence.
Question : If your class overrides the hashCode method , how will you get the
int value of default hashCode() .
Ans : Use System.identityHashCode(obj1);
Question: how can you make your class immutable ?
Ans: Declare all variables as private, and do not provide setters and getters.
Subscribe to:
Posts (Atom)
AWS certification question
AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...
-
Here I am going to discuss some of the differences between Text and String class in Hadoop. Text class lies in the package: import org.ap...
-
Q: What is the library you used in spring to connect to database? Spring JDBC template. Q: What and all involved in fetching the data ...
-
Question : What is GWT ? Answer : Google Web Tool Kit(GWT) is a open source Java Development Framework, use to develop Ajax(Asynchronous...