Tuesday, April 13, 2010

Displaying IP Address of the system using Runtime

Runtime is a class which is specifically made for the purpose to run any kind of process using "exec" method

import java.io.*;
class DisplayIP{
public static void main(String args[]){

Runtime runtime = Runtime.getRuntime();
try{
Process process = runtime.exec("cmd.exe /c ipconfig");
InputStream in = process.getInputStream();
int ch = in.read();
while(ch!=-1){
System.out.print((char)ch);
ch=in.read();
}
in.close();
}
catch(IOException e){
System.out.println(e);
}
}
}

Still any doubt !!! check out by clicking the link
http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html
Cheers
Shyamala

Deep Copy vs Shallow Copy in C++

Usually in c++ we will be more confused about these constructors about deep copying & shallow copying.....

Here is a simple example where there is a class called String where we have to strings which is used to store firstname ,lastname and the age of the person.

Where in case of this I am creating the Object of String with the name s1 which after reading the details I am assigning it to one more Object s2.



#include<iostream.h>
#include<string.h>
#include<conio.h>

class String
{
char *firstname; // declaring pointer to store string
char *lastname;
int age;

public:
String()
{
firstname = new char[30];
lastname = new char[30];
age=0;
}
void read()
{
cout<<"\n Enter name:";
cin>>firstname>>lastname;
cout<<"\n age :";
cin>>age;
}
void print()
{
cout<<"\n name is "<<firstname<<" "<<lastname;
cout<<"\n Age :"<<age;
}
~String()
{
delete[]firstname;
delete[]lastname;
}
};

void main()
{
clrscr();

String s1;
s1.read();
s1.print();

String s2=s1; //assigning the object s1 to s2
s2.print();

s1.read();

s1.print();
s2.print();

getch();
}

U can observe after u make any changes to the s1 object with the Strings that is firstname,lastname it will be reflected in s2 object also.

So by default there is one copy constructor given for you in your c++ classes which will be of this prototype which just assigns the values using "=" operator.

Prototype of Copy constructor:
ClassName(ClassName &Object){
}
This is "SHALLOW COPYING"
For the above class :
String(String &s){
firstname=s.firstname;
lastname=s.lastname;
age = s.age;
}

To over come this problem where it should be allocated with the own memory allocations just keep this below code and observe which is called "DEEP COPYING"

String(String &s)
{
cout<<"\n copying......";
int l1= strlen(s.firstname)+1;
int l2= strlen(s.lastname)+1;
firstname = new char[l1];
lastname = new char[l2];
strcpy(firstname,s.firstname);
strcpy(lastname,s.lastname);
age=s.age;
}


SHALLOW COPYING IS DEFAULT DONE .......WHICH OVERLOADS "=" operators.

DEEP COPYING YOU SHOULD WRITE ON YOUR OWN!!

Cheers
Shyamala

Saturday, April 10, 2010

SCJP Questions

Question 1 :
class Demo{
public static void main(String args){
System.out.println("welcome to java");
}
}

What will be the output of the program:
a) Error at compile time b) Error at Runtime
c) prints welcome to java d) No Error and no output

===============================================================
Question 2:
Which of the following are legal statements in Java?
a) float f=1/3; b) float f=1.01;
c) int i=1/3; d) double d=999d;

===============================================================
Question 3:
What will happen when you compile and run the following code?
public class MyClass{
static int i;
public static void main(String argv[]){
System.out.println(i);
}
}
a) Error Variable i may not have been initialized c) 1
b) null d) 0

===============================================================
Question 4:
class Demo{
static void print(){
System.out.println("Hello");
}
public static void main(String args){
Demo d =null;
d.print();
}
}

a) Error at compile time b) Error at Runtime
c) prints Hello d) No Error and no output

===============================================================Question 5:
Which of the following statements are true?
a) A byte can represent between -128 to 127
b) A byte can represent between -256 to 256
c) A byte can represent between -127 to 128
d) A char can represent between -2x2 pow 16 2 x2 pow 16 - 1

===============================================================Question 6:
Which of the following statements are true?
a) Constructors cannot have a visibility modifier
b) Constructors can only have a primitive return type
c) Constructors can be marked public and protected, but not private
d) Constructors are not inherited

===============================================================Question 7:
What will happen when you attempt to compile and run the following class?
class Base{
Base(int i){
System.out.println("Base");
}
}
class Severn extends Base{
public static void main(String argv[]){
Severn s = new Severn();
}
void Severn(){
System.out.println("Severn");
}
}
a) Compilation and output of the string "Severn" at runtime
b) Compilation and no output at runtime
c) Compile time error
d) Compilation and output of the string "Base"

===============================================================Question 8:
Given the following code
class Base {}

class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
//Here
}
}
What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string "Agg"?
a) System.out.println(a.getFields());
b) System.out.println((Base) a.getFields());
c) System.out.println(a.name);
d) System.out.println( ((Agg) a).getFields());

===============================================================Question 9:
What will happen when you attempt to compile and run the following code?
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
a) Compile time error b) Output of 1
c) Output of 2 d) Output of 0

===============================================================Question 10:
Given the following class
public class Ombersley{
public static void main(String argv[]){
boolean b1 = true;
if((b1 ==true) || place(true)){
System.out.println("Hello Crowle");
}
}

public static boolean place(boolean location){
if(location==true){
System.out.println("Borcetshire");
}
System.out.println("Powick");
return true;
}
}
What will happen when you attempt to compile and run it?
a) Compile time error
b) Output of Borcetshire and Powick followed by "Hello Crowle"
c) Output of "Hello Crowle"
d) No output
===============================================================Question 11:
What will happen when you attempt to compile and run the following code?
public class Sandys{
private int court;
public static void main(String argv[]){
Sandys s = new Sandys(99);
System.out.println(s.court);
}
Sandys(int ballcount){
court=ballcount;
}
}
a) Compile time error, the variable court is defined as private
b) Compile time error, s is not initialized when the System.out method is called
c) Compilation and execution with no output
d) Compilation and run with an output of 99
===============================================================
Question 12:
Given the following code
class Base{
static int oak=99;
}
public class Doverdale extends Base{
public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}
Which of the following if placed after the comment //Here, will compile and modify the value of the variable oak?
a) super.oak=1; b) Base.oak=22;
c) oak=33; d) oak=50.1;
===============================================================Question 13:
Which of the following statements are true?
a) A method cannot be overloaded to be less public in a child class
b) To be overridden a method only needs the same name and parameter types
c) To be overridden a method must have the same name, parameter and return types
d) An overridden method must have the same name, parameter names and parameter types
===============================================================Question 14:
You want to loop through an array and stop when you come to the last element. Being a good java programmer and forgetting everything you ever knew about C/C++ you know that arrays contain information about their size. Which of the following can you use?

a)myarray.length();
b)myarray.length;
c)myarray.size
d)myarray.size();
===============================================================Question 15:
What will happen when you attempt to compile and run this program
public class Outer{
public String name = "Outer";
public static void main(String argv[]){
Inner i = new Inner();
i.showName();
}//End of main
private class Inner{
String name =new String("Inner");
void showName(){
System.out.println(name);
}
}//End of Inner class
}
a) Compile and run with output of "Outer"
b) Compile and run with output of "Inner"
c) Compile time error because Inner is declared as private
d) Compile time error because of the line creating the instance of Inner
===============================================================Question 16:
Given the following class definition, which of the following methods could be legally placed after the comment //Here

public class Rid{
public void amethod(int i, String s){}
//Here
}
a)public void amethod(String s, int i){}
b)public int amethod(int i, String s){}
c)public void amethod(int i, String mystring){}
d) public void Amethod(int i, String s) {}
===============================================================Question 17:
public class Test7{
public Test7(){}
public Test7(Test7 ref){
this (ref,"Hai");
}
public Test7(Test7 ref,String str){
ref.Test7(str);
System.out.println("Hi");
}
public void Test7(String str){
System.out.println(str);
}
public static void main(String[] args){
Test7 t = new Test7();
Test7 t7 = new Test7(t);
}
}
a) HI b) hai
c) Hai Hi d) Hi Hai
===============================================================Question 18:
What will be the output of the following program?

class Sup1{
public Sup1(){
System.out.println("Hai");
}
private Sup1(String str){
System.out.println(str);
}
}

public class Test5 extends Sup1{
private Test5(String str){
System.out.println(str);
super();
}
public static void main(String[] args) {
Test5 t5 = new Test5("HI");
}
}

a) Hai,Hi,Hi b) Hai,Hi
c) Hi,Hi d) Compiler Error
===============================================================Question 19:
class VarArgOne {
public static void printArgs(String s, int ... i, String s) { //line1
for(int j : i) { //line 2
System.out.print(j + " " + s); //line 3
}
}
public static void main(String ... args) { //line 4
printArgs("exam", 12, 34, "scjp"); //line 5
}
}
a) Compilation fails due to error at line 1.
b) Compilation fails due to error at line 2.
c) Compilation fails due to error at line 4.
d) Compilation fails due to error at both line 1 and line 4.
e) Compiles fine and Prints output "12 scjp 34 scjp".
===============================================================Question 20:
class A{
private void print(){
System.out.println("===print in A===");
}
}
class B extends A{
void print(){
System.out.println("===print in B===");
}
}
class M{
public static void main(String args[]){
A ref = new B();
ref.print();
}
}
What happens when we try to compile the above program:
a)Error at compile time b)Error at run time
c)prints print in A d) prints print in B
===============================================================Question 21 :
interface I{
void print();
}
class A{
static void print(){
System.out.println("===print in A===");
}
}
class B extends A implements I{
public void print(){
System.out.println("===print in A===");
}
}
class M{
public static void main(String args[]){
A ref = new B();
ref.print();
}
}
What will be the output & Write explanation?
===============================================================Question 22:
What all gets printed when the following code is compiled and run? Select the three correct answers.
public class xyz {
public static void main(String args[]) {
for(int i = 0; i < 2; i++) {
for(int j = 2; j>= 0; j--) {
if(i == j) break;
System.out.println("i=" + i + " j="+j);
}
}
}
}
===============================================================Question 23:
What is the result of compiling and running the following program. Select the one correct answer.
class test {
public static void main(String args[]) {
int[] arr = {1,2,3,4};
call_array(arr[0], arr);
System.out.println(arr[0] + "," + arr[1]);
}
static void call_array(int i, int arr[]) {
arr[i] = 6;
i = 5;
}
}
a. 1,2 b. 5,2
c. 1,6 d. 5,6
===============================================================Question 24:
Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer.
a. test();
b. super.test();
c. super.super.test();
d. ::test();
e. C.test();
f. It is not possible to invoke test() method defined in C from a method in A.
===============================================================Question 25:
Which of the following are legal array declarations. Select the three correct answers.
a. int i[5][]; b. int i[][];
c. int []i[]; d. int i[5][5];
e. int[][] a;
===============================================================
ALL THE BEST

Difference Between String VS StringBuffer VS StringBuilder

Shown below is an example that concatanates 10000 strings using the '+' operator, StringBuffer, and StringBuilder, and prints the operation time in nanoseconds. The ability to get time in nanoseconds is another feature that was added in JDK 1.5.

public class StringConcatanations {
public static final int MAX_ITER = 10000;

public static void main(String[] args) {
concatenate();
concatenateWithStringBuffer();
concatenateWithStringBuilder();
}

public static void concatenate() {
System.out.println("Concatanating using the + operator");
String s1 = "";
long s1Time = getNanoTime();
for(int i=0;i) {
s1 = s1 + "abc";
}
long e1Time = getNanoTime();
System.out.println("Time: " + (e1Time - s1Time));
}

public static void concatenateWithStringBuffer() {
System.out.println("Concatanating using StringBuffer");
StringBuffer sb = new StringBuffer();
long s2Time = getNanoTime();
for(int i=0;i) {
sb.append("abc");
}
long e2Time = getNanoTime();
System.out.println("Time: " + (e2Time - s2Time));
}

public static void concatenateWithStringBuilder() {
System.out.println("Concatanating using StringBuilder");
StringBuilder sBuilder = new StringBuilder();
long s3Time = getNanoTime();
for(int i=0;i) {
sBuilder.append("abc");
}
long e3Time = getNanoTime();
System.out.println("Time: " + (e3Time - s3Time));
}

public static long getNanoTime() {
return System.nanoTime();
}

}


Output:
Concatanating using the + operator
Time: 744597428
Concatanating using StringBuffer
Time: 1685131
Concatanating using StringBuilder
Time: 1317206


Check out the time taken to append the String using "+" operator!!

As you can see, if you do not need thread safety, StringBuilder yeilds the best result, followed by Stringuffer, followed by the '+' operator. However, if you do need thread safety, then StringBuffer is your natural choice.

Cheers

Shyamala