These questions are basically preparation for campus drive.
Mostly these problems prefer for written exams.
Mostly these problems prefer for written exams.
1. Write a function to convert a decimal number string into a binary string. For example, String A contains “123” which is decimal value 123. Convert this string into a binary string
Solution:-
//This function gives exception ArrayIndexOutOfBoundsException
public void DecimalToBinary( int number)
{
int bin[] = new int[40];
int i=0;
while(number>0)
{
bin[i++] = number%2;
number=number/2;
}
System.out.println("Convrted binary number of 123 is :- ");
for(int k= i-1;k>=0;k--)
{
System.out.print(bin[k]);
}
}
Output-
//converted binary number of 123 is :-
1111011
1. Write a java program to take a read a csv file and print the same in output file. This csv file contains three column having numeric values. The output file should contain the sum of these numeric values
Solution:-
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
public class ReadCSVfile_and_Print {
public static void main(String[] args) throws Exception {
File file = new File("v3mobile.csv");
List<String> lines = Files.readAllLines(file.toPath(),
StandardCharsets.UTF_8);
for (String line : lines) {
String[] array = line.split(",", -1);
System.out.println(array[0]);
}
}
}
//Second Solution
import java.io.BufferedReader;
import java.io.FileReader;
public class ReadCSVfile_and_Print {
public static void main(String[] args) throws Exception {
String splitBy = ",";
BufferedReader br = new BufferedReader(new FileReader("v3mobile.csv"));
String line = br.readLine();
while(line!=null){
String[] b = line.split(splitBy);
System.out.println(b[0]);
}
br.close();
}
}
3. Write a program to read value from a DB. The DB schema is as follows
a. User name: root
b. Password: root
c. Table name: test
d. Schema
i. Emp name varchar 200
ii. Emp age int 10
Solution:-
import java.sql.*;
import java.util.*;
public class JavaDatabaseReaddata {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded .");
Connection con =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from test");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getInt(2));
}
rs.close();
st.close();
con.close();
}
}
4. Write a java program to call a HTTP URL and print the output.
a. URL is http://127.0.0.1:3300/test?value=p .
Solution:-
//For Server Coding
import java.net.*;
import java.io.*;
import java.util.*;
class tcpdateserver
{
public static void main(String args[])
{
ServerSocket ss = null;
Socket cs;
PrintStream ps;
BufferedReader dis;
String inet;
try{
ss = new ServerSocket(4444);
System.out.println("PressvCtrl+c to quit ");
while(true)
{
cs = ss.accept();
ps = new PrintStream(cs.getOutputStream());
Date d=new Date();
ps.println(d);
dis = new BufferedReader(new InputStreamReader(cs.getInputStream()));
inet=dis.readLine();
System.out.println("Client System/Ip address is : "+inet);
ps.close();
dis.close();
}
}
catch(IOException e)
{
System.out.println("The Exception is : "+e);
}
}
}
//For Client Coding
import java.net.*;
import java.io.*;
class tcpdateclient
{
public static void main(String args[])
{
try{
Socket soc;
BufferedReader dis;
String sdate;
PrintStream ps;
InetAddress ia=InetAddress.getLocalHost();
if(args.length == 0)
soc=new Socket(InetAddress.getLocalHost(),4444);
else
soc=new Socket(InetAddress.getByName(args[0]),4444);
dis = new BufferedReader(new InputStreamReader(soc.getInputStream()));
sdate = dis.readLine();
System.out.println("The date/time on server is : "+sdate);
ps=new PrintStream(soc.getOutputStream());
ps.println(ia);
ps.close();
}
catch(IOException e)
{
System.out.println("The Exception is : "+e);
}
}
}
//Output is :
//Server Computer
A:\Pravesh Tutorials\V3Mobile_For_Internshps>java tcpdateserver
PressvCtrl+c to quit
Client System/Ip address is : 127.0.0.1:3300/test?value=p .
//Cliect Computer
A:\Pravesh Tutorials\V3Mobile_For_Internshps>java tcpdateclient
The date/time on server is : Mon Mar 12 18:04:46 IST 2018
5. Write a java program to store the values in Hashmap (key, store) and then search those given value from the Hashmap and print it.
a. Name is key, age is value….so store the following
i. (Ram, 36), (shyam,30), (dharam,10) (ravi,)
Solution:-
import java.util.*;
class HashMapStore
{
public static void main(String args[])
{
HashMap<String, Integer> hm = new HashMap<String, Integer>();
//store the values in Hashmap
hm.put("Ram",36);
hm.put("Shyam",30);
hm.put("dharam",10);
hm.put("ravi",null);
System.out.println(hm);
//search these value from the Hashmap and print it.
Iterator entries = hm.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String key = (String)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
}
}
6. Write a HTTP server process that take the above URL and print the receive value and send back the response.
a. The response is I received p.
Solution:-
//For Server Coding
import java.net.*;
import java.io.*;
import java.util.*;
class tcpdateserver
{
public static void main(String args[])
{
ServerSocket ss = null;
Socket cs;
PrintStream ps;
BufferedReader dis;
String inet;
try{
String str = " ";
String str2 = " ";
ss = new ServerSocket(4444);
System.out.println("PressvCtrl+c to quit ");
while(true)
{
cs = ss.accept();
ps = new PrintStream(cs.getOutputStream());
Date d=new Date();
ps.println(d);
dis = new BufferedReader(new InputStreamReader(cs.getInputStream()));
inet=dis.readLine();
System.out.println("Client System/Ip address is : "+inet);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
str = din.readUTF();
System.out.println("Client Says : "+str);
dout.writeUTF(str2);
dout.flush();
ps.close();
din.close();
dout.close();
dis.close();
}
}
catch(IOException e)
{
System.out.println("The Exception is : "+e);
}
}
}
//For Client Coding
import java.net.*;
import java.io.*;
class tcpdateclient
{
public static void main(String args[])
{
String str = " ";
String str2 = " ";
try{
Socket soc;
BufferedReader dis;
String sdate;
PrintStream ps;
InetAddress ia=InetAddress.getLocalHost();
if(args.length == 0)
soc=new Socket(InetAddress.getLocalHost(),4444);
else
soc=new Socket(InetAddress.getByName(args[0]),4444);
dis = new BufferedReader(new InputStreamReader(soc.getInputStream()));
sdate = dis.readLine();
System.out.println("The date/time on server is : "+sdate);
ps=new PrintStream(soc.getOutputStream());
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
str = din.readUTF();
System.out.println("Server Says : "+str2);
str2 = br.readLine();
dout.writeUTF(str2);
dout.flush();
ps.println(ia);
ps.close();
dout.close();
din.close();
}
catch(IOException e)
{
System.out.println("The Exception is : "+e);
}
}
}
//Output is :
//Server Computer
A:\Pravesh Tutorials\V3Mobile_For_Internshps>java tcpdateserver
Client Says :
PressvCtrl+c to quit
Client System/Ip address is : 127.0.0.1:3300/test?value=p .
//Cliect Computer
A:\Pravesh Tutorials\V3Mobile_For_Internshps>java tcpdateclient
The date/time on server is : Mon Mar 12 18:04:46 IST 2018
Server Says :
The response is I received p.
7. Write a program that take input from command line and print all the received input.
a. For example the java program name is X then the user will run the program as
i. Java X A B C
ii. The output is à There are 3 input, and inputs are A, B and C.
Solution:-
public class X {
public static void main (String[] args) {
System.out.print("The output is -> There are 3 input, and inputs are "+args[0]+","+args[1]+" and "+args[2]);
}
}
//If We enter java X A B then it gives Exception ArrayIndexOutOfBoundsException
A:\Pravesh Tutorials\V3Mobile_For_Internshps>java X A B
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at X.main(X.java:3)
8. Write a SQL query to find the age is less than 30 and name start with S
a. Schema : Table name : test, Column (age, int (10), name varchar (10))
Solution:-
Select Column from test where age<30 and name LIKE '[S]%';
//Also use Like
Select Column from test where age<30 and name LIKE('%S');
9 What do you mean by platform independence?
Platform independence means that we can write and compile the java
code in one platform (eg Windows) and can execute the class in any other
supported platform eg (Linux,Solaris,etc).
10. Is Java a pure object oriented language?
Java uses primitive data types and hence is not a pure object-oriented
language.
9 What do you mean by platform independence?
Platform independence means that we can write and compile the java
code in one platform (eg Windows) and can execute the class in any other
supported platform eg (Linux,Solaris,etc).
10. Is Java a pure object oriented language?
Java uses primitive data types and hence is not a pure object-oriented
language.
Be First to Post Comment !
Post a Comment