public class ImageScaling{
public BufferedImage getScaledInstance(final BufferedImage img,
final int targetWidth, final int targetHeight, final Object hint,
final boolean higherQuality) {
final int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
: BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = img;
int w, h;
if (higherQuality) {
// Use multi-step technique: start with original size, then
// scale down in multiple passes with drawImage()
// until the target size is reached
w = img.getWidth();
h = img.getHeight();
} else {
// Use one-step technique: scale directly from original
// size to target size with a single drawImage() call
w = targetWidth;
h = targetHeight;
}
do {
if (higherQuality && w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}
if (higherQuality && h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
}
final BufferedImage tmp = new BufferedImage(w, h, type);
final Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();
ret = tmp;
} while (w != targetWidth || h != targetHeight);
return ret;
}
public static void main(final String[] args) {
final File file = new File("c:\\fileImg.jpg");
try {
final BufferedImage img = ImageIO.read(file);
ImageIO.write(new ImageUtil().getScaledInstance(img, 72, 72,
RenderingHints.VALUE_INTERPOLATION_BICUBIC, true), "jpg",
new File("c:\\scaledImage.jpg"));
} catch (final IOException e) {
// AUTO Auto-generated catch block
e.printStackTrace();
}
}
}
Saturday, November 19, 2011
Friday, November 18, 2011
Java Experienced Interview Questions
Core Java
1) Difference between abstraction and encapsulation? Give some examples from your project.
2) There are two interfaces A and B, interface A has a variable i with value 10 and interface B also has variable i with value 20. There is a class that implements both interfaces in the constructor of the class there is statement System.out.println(i); what will be the output of this.
Code:
interface A {
public static final int i = 10;
}
interface B {
public static final int i = 10;
}
public class Test implements A,B
{
Test()
{
System.out.println(i);
}
}
what will be the output?
3) What are inner classes?
4) How will you initialize a regular inner class, method local class and static inner class?
5) How will you create Custom Exception?
6) Can I create custom exception by extending throwable class.
7) What is the property of Set interface.
8) Can we have constructor of abstract class. If yes then why we can not initialize abstract class using new keyword?
9) For a class Person I have overridden equals and hashcode methods of object class and for different objects of Person I am returing same hashcode every time like
public int hashCode()
{
return 1;
}
and I have added 4 different objects of Person class in HashMap then what will be the size of map and what will happen if we call get method of Map and passed object of Person in that?
10) What will be your approach to identify circular reference in bean classes using reflection?
11) Difference between HashTable and HashMap.
12) Difference between checked and un-checked exceptions.
13) Write code to create singleton class.
14) What is Generics.
15) What is static.
16) What will be the output of the following program
class Test {
static{
System.out.println("Static");
}
Test()
{
System.out.println("Constructor");
}
public static void main(String[] args) {
new Test();
new Test();
}
}
17) What will be the output of the following program and why
class Test {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
Integer j1 = 128;
Integer j2 = 128;
if(i1 == i2) {
System.out.println("i1 == i2");
}
else {
System.out.println("i1 != i2");
}
if(j1 == j2) {
System.out.println("j1 == j2");
}
else {
System.out.println("j1 != j2");
}
}
}
18) How HashMap handle collission in HashCode?
19) Comparison is done by equals method then why we need hashcode or what is the purpose of hashcode in collections?
20) Connection is an Interface and we can not initialize interface in java and DriverManager is a class we call static method getConnection of DriverManager then how does DriverManager.getConnection(....) give connection object?
21) What will be out put of the following program
class A {
public A(int a)
{
System.out.println("Constructor of A");
}
}
class B {
public B(int b) {
System.out.println("Constructor of B");
}
}
class C {
public static void main(String[] args) {
B b = new B(10);
A a = new A(20);
}
}
22) In Java, how will you declare a variable x such that the following two conditions are satisfied?
x = ++x
x = x + 1 is
23) What will happen if we try to call static method from instance of class using .(dot) operator?
24) What will be the output of the following program
class Test {
private static int a = 10;
public static void staticThis()
{
System.out.println(this.a);
}
public static void main(String[] args) {
Test.staticThis();
}
}
25) What will be the output of the following program
class Test {
public static void main(String[] args) {
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error(); /* Line 22 */
}
}
26) Consider two simple JAVA programs where input parameters for one of the programs depend upon the output of the other program. In such scenario, will multithreading help in improving performance ?
Subscribe to:
Comments (Atom)
Spring Boot Config Server and Config Client.
In Spring cloud config we can externalise our configuration files to some repository like GIT HUT, Amazon S3 etc. Benefit of externalising ...
-
As we know that TreeSet by default store the unique value it doesn’t check case of the value that is being added. For example if...
-
We can extract text from pdf file using itext 2.1.6 PdfReader readerN = new PdfReader("pdfFilename"); OutputStream...
-
We mostly use hibernate ‘s sessionFactory.openSession() method which keeps track of all events of hibernate along with managing first ...