Interview Question

Interview Question

1)If we give S.O.P. statement after finally block shall it prints or not?
2)Write a program in java to read a file & write in to another file?
3)Write a program taking two arrays and compare those two and print, without using functions?
View Answers

July 7, 2010 at 12:52 PM

Hi Friend,

1)It will print.

2)ReadFile.java:

import java.util.*;
import java.io.*;
import java.text.*;
import java.util.regex.*;

public class ReadFile{
public static void main(String args[]) throws IOException{
try {
BufferedReader reader = new BufferedReader(new FileReader("c://a.txt";));
Pattern pat = Pattern.compile(",(\".+?\")");
String line=null;
while ((line=reader.readLine())!=null){
Matcher matcher = pat.matcher(line);
StringBuffer sb = new StringBuffer();
while (matcher.find()){
matcher.appendReplacement(sb,","+matcher.group(1).replaceAll(",","&comm"));
}
matcher.appendTail(sb);
String[] fields = sb.toString().split(",");
for (int i=0;i<fields.length;i++){
String out = fields[i]!=null ? fields[i].replaceAll("&comm",",") : "";
FileWriter fstream = new FileWriter("out.txt",true);
BufferedWriter output = new BufferedWriter(fstream);
output.write(out+",");
output.newLine();
output.close();
}
}
}catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}

3)CompareArrays.java:

import java.util.*;

public class CompareArrays{
public static void main(String[]args){
String[] arr1={"a","fdf","h","d","e"};
String[] arr2={"a","b","ch"};
List<String> a1 = Arrays.asList(arr1);
List<String> a2 = Arrays.asList(arr2);
int array1Size = a1.size();
int array2Size = a2.size();

Set s = new HashSet();
s.addAll(a1);
s.addAll(a2);
ArrayList list = new ArrayList();
for(int i=0; i<a2.size(); i++) {
if(!a1.contains(a2.get(i))) {
list.add(a2.get(i));
}
}
for(int i=0; i<a1.size(); i++) {
if(!a2.contains(a1.get(i))) {
list.add(a1.get(i));
}
}
System.out.println("Elements which are different: ");
System.out.println("");
for(int i = 0; i < list.size(); i++){
System.out.print((String)list.get(i)+" ");
}
}
}

Thanks









Related Tutorials/Questions & Answers:
struts interview Question - Struts
struts interview question and answer java  struts interview question and answer java
Interview question.
Interview question.  Program to read the integers from the standard input (until it gets a negative number) and puts them in to an array.after that it calls process Array on the array,and then prints the content of the array
Advertisements
Interview question
Interview question  what is mean by Dynamic class loading
struts interview question in ibm
struts interview question in ibm  what is the use of "format" attribute
interview question
interview question  write a method that takes and return an object of your own type?   Hi Friend, Try the following code:ADS_TO_REPLACE_1 class TakeAndReturnObject{ public static String getLargestWord(String st
java interview question and answer - Java Interview Questions
java interview question and answer   sir, i want one mark java interview question and answers
interview question - Java Interview Questions
interview question  hello i want technical interview question in current year   Hi Friend, Please visit the following links: http://www.roseindia.net/interviewquestions/ http://www.roseindia.net
interview question - Servlet Interview Questions
according to your experience.And for collection of Java Servlet Interview Question you...interview question  What is Servlet? Need interview questions on Java... and HTTP Servlet.While going through an interview you could be asked for number
Interview question link - Java Beginners
Interview question link  Hi, I want JAVA+J2EE interview question and answer please suggesion me link
Core Java Interview Question, Interview Question
Core Java Interview Question   ....   Core Java Interview Question Page 1 How could Java... to a file?      Core Java Interview Question Page 2
Java Interview Question
Java Interview Question  Java program Interview Question :i have a file which contains characters and integers in mixed format. for example :--> abd12 g156bcd etc Now how would i rearrange the file sothat the characters
Interview Question - Java Interview Questions
Interview Question  I need Interview Questions on Java,J2EE Pls help me
Interview question at Software AG
Interview question at Software AG  write a program to print a string which take input from 0 to 9 like "0122233455" and give o/p as 0,1,2,3,4,5** in java .... Any one plz help me to solve this prog
one interview question - Java Interview Questions
one interview question   Hello, In one of my interview the interviewer ask one question "what are the problems you faced during project... for this question. Thanks
Interview question - Java Interview Questions
Interview question   Hi Friends, Give me details abt synchronization in interview point of view. I mean ow to explain short and neat. Thanks
General Interview Question - IDE Questions
General Interview Question  Please provide soln for this question(wt are all the concepts we hav to add for this question).Explain Architecture of ur project in brief
Interview question - JSP-Interview Questions
Interview question   Hi Friend, Can we inherit interface in JSP. Thank u in advance
Interview question - Java Interview Questions
Interview question   Hi Friends, I am new to java, can u tell me what is class ,object and instance with example. For example : class raja , raja r = new raja(); ,here raja is class and r is instance
java interview question
java interview question  when will we use abstraction or interface
java interview question
java interview question  what is the usage of abstract class
Core Java Interview Question, Interview Question
Core Java Interview Question Page 27       Modifiers Question: What are access modifiers... modifier then it is considered as Friendly Question: Can protected or friendly
Core Java Interview Question, Interview Question
Core Java Interview Question Page 14       Question: What is clipping? Answer: Clipping..._TO_REPLACE_1 Question: What is a native method? Answer: A native method is a method
Core Java Interview Question, Interview Question
Core Java Interview Question Page 12       Question: What is a transient variable? Answer... Question: Which containers use a border Layout as their default layout? Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 17       Question: What is the difference between... application window that can have a menu bar. ADS_TO_REPLACE_1 Question: Which class
Core Java Interview Question, Interview Question
Core Java Interview Question Page 18       Question: Is &&= a valid Java operator? Answer: No, it is not. ADS_TO_REPLACE_1 Question: Name the eight
Core Java Interview Question, Interview Question
Core Java Interview Question Page 16       Question: Which containers may have a MenuBar? Answer: Frame ADS_TO_REPLACE_1 Question: How are commas used
Core Java Interview Question, Interview Question
Core Java Interview Question Page 15       Question: What invokes a thread's run() method... executed. ADS_TO_REPLACE_1 Question: What is the difference between the Boolean
Core Java Interview Question, Interview Question
Core Java Interview Question Page 21       Question: How are the elements of a GridLayout... and are laid out using the squares of a grid. ADS_TO_REPLACE_1 Question: What an I/O
Core Java Interview Question, Interview Question
Core Java Interview Question Page 2       Question: Does it matter... the IOException. Exception's subclasses have to be caught first. ADS_TO_REPLACE_1 Question
Core Java Interview Question, Interview Question
Core Java Interview Question Page 13       Question: What is an Iterator interface? Answer.... ADS_TO_REPLACE_1 Question: What is the difference between the >> and >>
Core Java Interview Question, Interview Question
Core Java Interview Question Page 30       Objects and Classes Question: What's... while regular methods could be called many times. Question: What
Core Java Interview Question, Interview Question
Core Java Interview Question Page 19       Question: What is the difference between static... variables take on unique values with each object instance. ADS_TO_REPLACE_1 Question
Interview Question - Java Interview Questions
Interview Question  1)If we give S.O.P. statement after finally block shall it prints or not? 2)Write a program in java to read a file & write in to another file? 3)Write a program taking two arrays and compare those two
Core Java Interview Question, Interview Question
Core Java Interview Question Page 20       Question: Name four Container classes. Answer..._TO_REPLACE_1 Question: What is the difference between a Choice and a List? Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 22       Question: What is a compilation unit? Answer: A compilation unit is a Java source code file. ADS_TO_REPLACE_1 Question: What
Interview question - Date Calendar
Interview question   Hi Friends, can u give me sample code for validating date. Thank u
Core Java Interview Question, Interview Question
Core Java Interview Question Page 32   ... Question: What is the ultimate ancestor of all java classes Answer: Object class is the ancestor of all the java classes Question: What
Core Java Interview Question, Interview Question
Core Java Interview Question Page 23       Question: To what value is a variable... is false. ADS_TO_REPLACE_1 Question: Can try statements be nested? Try statements
Core Java Interview Question, Interview Question
Core Java Interview Question Page 26       Language Fundamentals Question: How... Question: What types of values does boolean variables take It only takes
Core Java Interview Question, Interview Question
Core Java Interview Question Page 24       Question: Why do we need public static void main... class did not have a public constructor? Question: What is the difference
Core Java Interview Question, Interview Question
Core Java Interview Question Page 25       Question: What are null or Marker..., this is to convey the compiler that they have to be treated differently Question
Core Java Interview Question, Interview Question
Core Java Interview Question Page 11       Question: What is serialization ?  Answer.../IP socket. ADS_TO_REPLACE_1 Question: What does the Serializable interface do
Core Java Interview Question, Interview Question
Core Java Interview Question Page 29       Flow Control and exception Question: What... at the end of the body Question: When do you use continue and when do you use
Core Java Interview Question, Interview Question
Core Java Interview Question Page 28       Conversion Casting and Promotion Question: What... primitive types to be accessed as objects. Question: What are the four general
Interview question - Development process
Interview question   Hi Friends, Can we create scriptless jsp, if so explain me how. can we access database by using javascript only
Core Java Interview Question, Interview Question
Core Java Interview Question Page 31       Threads Question: Where does java thread... The java language and virtual machine Question: What is the difference
Core Java Interview Question, Interview Question
Core Java Interview Question Page 4       Question: What is the difference between checked... | +------------------+ / | | \ \_________________/ unchecked Question: Explain garbage collection ?  Answer: Garbage
Core Java Interview Question, Interview Question
Core Java Interview Question Page 9       Question: What are the uses of Serialization... objects between the servers in a cluster. Question: what is a collection ? 
Core Java Interview Question, Interview Question
Core Java Interview Question Page 5       Question: What are different types...) { // do something } } ); Question: What are the uses of Serialization? Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 7       Question: What... associated with TCP/IP socket. ADS_TO_REPLACE_1 Question: What does

Ads