Thursday, 28 January 2016

Java Interview Questions - 9

Below are few java interview questions where candidate need to be more clear on basics and how it works when they asked simples questions. All these questions are programmatic questions and need to answer whether program will compile and run without any error and next what will be the out?


package com.pract;

import java.lang.reflect.Method;

class DeclaredMethodsPractBase {

 public void myParent1(){};
 protected void myParent2(){};
 private void myParent3(){};
}

public class DeclaredMethodsPract extends DeclaredMethodsPractBase{
 
 public void myChild11(){};
 protected void myChild12(){};
 private void myChild13(){};
 
 public static void main(String[] args) throws ClassNotFoundException {
  
  Class<?> classs = Class.forName("com.pract.DeclaredMethodsPract");
  
  Method[] methods = classs.getDeclaredMethods();
  
  for (Method method : methods) {
   System.out.print(method.getName() + ", ");
  }
 }
}

1. Runtime error
2. main, myChild11, myChild12, myChild13, myParent1,
3. main, myChild11, myChild12, myChild13, myParent1, myParent2,
4. main, myChild11, myChild12, myChild13, myParent1, myParent2, myParent3,



public class EnumPract {

 enum Numbers{ONE, TWO, THREE};
 
 public static void main(String[] args) {
  
  System.out.print((Numbers.ONE == Numbers.ONE) + ", ");
  System.out.print(Numbers.ONE.equals(Numbers.ONE));
 }
}

1. Compile time error
2. true, false
3. false, false
4. false, true
5. true, true



public class InternPract {

 public static void main(String[] args) {
  String a1 = "Java";
  String a2 = new String("Java");
  String a3 = a1.intern();
  
  System.out.print((a1 == a2) + ", "+(a1 == a3));
 } 
}

1. false, true
2. true, true
3. true, false
4. false, false



import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class MapObjectPract {

 public static void main(String[] args) {
  
  Map<String, String> map = new HashMap<String, String>();
  
  System.out.print((map instanceof Object) + ", ");
  System.out.print((map instanceof Map) + ", ");
  System.out.print((map instanceof HashMap) + ", ");
  System.out.print(map instanceof Collection);
 }
}

1. true, false, true, false
2. false, true, true, false
3. true, true, true, false
4. false, false, true, false



public class OperatorPract {

 public static void main(String[] args) {
  
  int i = 0;
  i = i++ + method(i);
  System.out.print(i);
 }
 
 static int method(int i){
  System.out.print(i+":");
  return 0;
 }
}

1. 1:0
2. 1:1
3. 0:1
4. 0:0



class Recursion{
 int method(int n){
  int result = method(n - 1);
  return result;
 }
}

public class RecuPract {

 public static void main(String[] args) {
  Recursion obj = new Recursion();
  int val = obj.method(10);
  System.out.println("completed : "+ val);
 }
}

1. completed : 0
2. compile time error
3. Runtime error
4. completed : -1



class Test implements Runnable{
 
 @Override
 public void run() {
  System.out.print("HELLO : "+Thread.currentThread().getName() + ", ");
 }
}

public class RunnablePract {

 public static void main(String[] args) {

  Test obj = new Test();
  
  Thread t1 = new Thread(obj, "T1");
  Thread t2 = new Thread(obj, "T2");
  
  t1.start();
  t1.join();
  t2.start();
 }
}

1. Runtime error
2. HELLO : T1, HELLO : T2,
3. HELLO : T2, HELLO : T1,
4. Compile time error



import java.util.HashSet;

public class SetPract {

 public static void main(String[] args) {
  
  HashSet<Integer> set = new HashSet<Integer>();
  for(int i=0;i<100;i++){
   set.add(i);
   set.remove(i - 1);
  }
  
  System.out.println("SET SIZE ::: "+set.size());
 }
}

1. SET SIZE ::: 100
2. SET SIZE ::: 1
3. SET SIZE ::: 99
4. SET SIZE ::: 0



public class StringArrayPract {

 private String[] array;
 
 public StringArrayPract() {
  array = new String[10];
 }
 
 public void setArray(String val, int i){
  array[i] = val;
 }
 
 public String getArray(int i){
  return array[i];
 }
 
 public static void main(String[] args) {
  
  StringArrayPract obj = new StringArrayPract();
  System.out.println("VALUE IS : "+obj.getArray(5));
 }
}

1. Runtime error
2. VALUE IS : null
3. Compile time error
5. VALUE IS :



import java.util.TreeSet;

public class TreeSetPract {

 public static void main(String[] args) {
  
  TreeSet<String> tSet = new TreeSet<String>();
  tSet.add("one");
  tSet.add("two");
  tSet.add("three");
  tSet.add("one");
  
  for (String string : tSet) {
   System.out.print(string+", ");
  }
 }
}

1. one, two, three,
2. one, two, three, one
3. one, one, three, two,
4. one, three, two, 

read more.. "Java Interview Questions - 9"

Friday, 22 January 2016

Spring - Sending E-Mail with MailSender

Sending email using Spring "org.springframework.mail.javamail.JavaMailSenderImpl" class simplifies the process with JavaMail API. Below example will give step by step to send email by using Gmail SMTP server configurations.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.springframework.samples</groupId>
 <artifactId>SendingEmail</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <dependencies>

  <!-- Spring framework -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring</artifactId>
   <version>2.5.6.SEC03</version>
  </dependency> 

  <!-- Java Mail API -->
  <dependency>
   <groupId>javax.mail</groupId>
   <artifactId>mail</artifactId>
   <version>1.4.3</version>
  </dependency>

 </dependencies>
</project>

email.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host" value="smtp.gmail.com" />
  <property name="port" value="587" />
  <property name="username" value="USERNAME" />
  <property name="password" value="PASSWORD" />

  <property name="javaMailProperties">
   <props>
    <prop key="mail.smtp.auth">true</prop>
    <prop key="mail.smtp.starttls.enable">true</prop>
   </props>
  </property>
 </bean>

 <bean id="emailBean" class="com.app.email.Mailer">
  <property name="emailSender" ref="emailSender" />
 </bean>

</beans>

Mailer.java

package com.app.email;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class Mailer {

 private MailSender emailSender;
 
 //Setter for emailSender
 public void setEmailSender(MailSender emailSender) {
  this.emailSender = emailSender;
 }
 
 //Method to send email
 public void sendMail(String from, String to, String subject, String msg) {

  SimpleMailMessage simpleMsg = new SimpleMailMessage();
  
  simpleMsg.setFrom(from);
  simpleMsg.setTo(to);
  simpleMsg.setSubject(subject);
  simpleMsg.setText(msg);
  
  //Finally sending email using MailSender
  emailSender.send(simpleMsg); 
 }
}

SendEmail.java

package com.app.email;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SendEmail {

 public static void main(String[] args) {

  String from = "from@gmail.com";
  String to = "to@gmail.com";
  String subject = "Hello World !";
  String msg = "Hello World, How are you ???";

  ApplicationContext context = new ClassPathXmlApplicationContext("email.xml");

  try {
   Mailer mailer = (Mailer) context.getBean("emailBean");
   mailer.sendMail(from, to, subject, msg);

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}


Spring - Sending E-Mail with MailSender


read more.. "Spring - Sending E-Mail with MailSender"