Bird
Raised Fist0
NextJSframework~10 mins

Image optimization with next/image in NextJS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Next.js Image component correctly.

NextJS
import [1] from 'next/image';
Drag options to blanks, or click blank then click option'
APicture
BImage
Cimg
DNextImage
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'img' instead of 'Image'
Using incorrect component names like 'NextImage'
2fill in blank
medium

Complete the code to use the Image component with a source URL.

NextJS
<Image src=[1] alt="A scenic view" width={600} height={400} />
Drag options to blanks, or click blank then click option'
A"/scenic.jpg"
Bscenic.jpg
C'scenic.jpg'
D{scenic.jpg}
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the URL
Passing a variable without braces
3fill in blank
hard

Fix the error in the Image component by completing the missing prop for accessibility.

NextJS
<Image src="/logo.png" [1] width={100} height={100} />
Drag options to blanks, or click blank then click option'
Aalt="Company logo"
Btitle="Logo"
Caria-label="Logo image"
Dcaption="Logo"
Attempts:
3 left
💡 Hint
Common Mistakes
Using title instead of alt
Omitting the alt attribute entirely
4fill in blank
hard

Fill both blanks to optimize the image loading behavior.

NextJS
<Image src="/photo.jpg" width={800} height={600} [1]=[2] />
Drag options to blanks, or click blank then click option'
Aloading
B"lazy"
C"eager"
Dpriority
Attempts:
3 left
💡 Hint
Common Mistakes
Using priority instead of loading
Using loading="eager" which loads immediately
5fill in blank
hard

Fill all three blanks to create a responsive Image component with a fixed layout and custom sizes.

NextJS
<Image src="/banner.png" layout=[1] width=[2] height=[3] alt="Banner image" />
Drag options to blanks, or click blank then click option'
A"fixed"
B1200
C400
D"responsive"
Attempts:
3 left
💡 Hint
Common Mistakes
Using layout="responsive" with fixed width and height
Passing width and height as strings

Practice

(1/5)
1. What is the main benefit of using next/image in a Next.js project?
easy
A. It only works with SVG images.
B. It converts images to text for SEO purposes.
C. It disables image loading to save bandwidth.
D. It automatically optimizes images for faster loading and better quality.

Solution

  1. Step 1: Understand the purpose of next/image

    The next/image component is designed to optimize images automatically, improving loading speed and quality.
  2. Step 2: Compare options with the purpose

    Only It automatically optimizes images for faster loading and better quality. correctly describes this benefit. Other options describe unrelated or incorrect features.
  3. Final Answer:

    It automatically optimizes images for faster loading and better quality. -> Option D
  4. Quick Check:

    Image optimization = D [OK]
Hint: Remember next/image optimizes images automatically [OK]
Common Mistakes:
  • Thinking next/image converts images to text
  • Believing it disables image loading
  • Assuming it only supports SVG
2. Which of the following is the correct way to import and use the Image component from Next.js for image optimization?
easy
A. import { Image } from 'react';
B. import Image from 'next/image';
C. import Img from 'next/image';
D. import Image from 'next/image';

Solution

  1. Step 1: Check import syntax

    The correct import for Next.js image optimization is import Image from 'next/image';.
  2. Step 2: Verify usage with required props

    The Image component requires src, alt, and explicit width and height for optimization. import Image from 'next/image';
    includes all correctly.
  3. Final Answer:

    import Image from 'next/image'; with src, alt, width, and height props -> Option B
  4. Quick Check:

    Correct import and props = B [OK]
Hint: Always import Image from 'next/image' with width and height [OK]
Common Mistakes:
  • Importing Image from 'react' instead of 'next/image'
  • Omitting width and height props
  • Using wrong import names
3. What will be the rendered output behavior of this Next.js code snippet?
import Image from 'next/image';

export default function Logo() {
  return (
    <Image src='/logo.png' alt='Company Logo' width={200} height={100} priority />
  );
}
medium
A. The image loads immediately with 200x100 size due to priority prop.
B. The image will not render because priority is invalid.
C. The image loads lazily by default with 200x100 size.
D. The image loads with default size ignoring width and height.

Solution

  1. Step 1: Understand the priority prop effect

    The priority prop forces the image to load immediately instead of lazy loading.
  2. Step 2: Check width and height usage

    Width and height props set the image size to 200x100 pixels, which will be respected.
  3. Final Answer:

    The image loads immediately with 200x100 size due to priority prop. -> Option A
  4. Quick Check:

    priority = immediate load = C [OK]
Hint: priority prop disables lazy loading for faster display [OK]
Common Mistakes:
  • Assuming priority disables image rendering
  • Thinking width and height are ignored
  • Confusing lazy loading default behavior
4. Identify the error in this Next.js image usage and how to fix it:
import Image from 'next/image';

export default function Profile() {
  return (
    <Image src='/profile.jpg' alt='User Profile' width='150' height='150' />
  );
}
medium
A. src path must be an external URL, not local path.
B. alt attribute is missing, causing accessibility issues.
C. width and height should be numbers, not strings.
D. Image component must be self-closing with a slash.

Solution

  1. Step 1: Check width and height prop types

    Next.js Image requires width and height as numbers, not strings. Here they are strings ('150').
  2. Step 2: Correct the prop types

    Remove quotes to pass numbers: width={150} and height={150}.
  3. Final Answer:

    width and height should be numbers, not strings. -> Option C
  4. Quick Check:

    width/height as numbers = A [OK]
Hint: Use curly braces for numeric width and height [OK]
Common Mistakes:
  • Passing width and height as strings
  • Ignoring alt attribute importance
  • Thinking local src paths are invalid
5. You want to optimize multiple images with different sizes responsively using next/image. Which approach correctly uses the sizes attribute to serve appropriate image sizes on different screen widths?
hard
A. <Image src='/photo.jpg' alt='Photo' width={800} height={600} sizes='(max-width: 600px) 100vw, 50vw' />
B. <Image src='/photo.jpg' alt='Photo' width='800' height='600' sizes='100vw' />
C. <Image src='/photo.jpg' alt='Photo' width={800} height={600} sizes='(min-width: 600px) 100vw, 50vw' />
D. <Image src='/photo.jpg' alt='Photo' width={800} height={600} />

Solution

  1. Step 1: Understand the sizes attribute usage

    The sizes attribute defines image display sizes for different viewport widths using media queries.
  2. Step 2: Analyze the options

    <Image src='/photo.jpg' alt='Photo' width={800} height={600} sizes='(max-width: 600px) 100vw, 50vw' /> correctly uses (max-width: 600px) 100vw, 50vw meaning full viewport width on small screens, half on larger screens. This is a common responsive pattern.
  3. Final Answer:

    Use sizes='(max-width: 600px) 100vw, 50vw' with numeric width and height. -> Option A
  4. Quick Check:

    Responsive sizes attribute = A [OK]
Hint: Use sizes with max-width media query for responsive images [OK]
Common Mistakes:
  • Passing width and height as strings
  • Using min-width instead of max-width incorrectly
  • Omitting sizes attribute for responsiveness
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard