Bird
Raised Fist0
NextJSframework~20 mins

Image optimization with next/image in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Next.js Image Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the rendered output of this Next.js Image component?
Consider this Next.js component using next/image:

import Image from 'next/image';

export default function Avatar() {
  return (
    <Image
      src="/avatar.png"
      alt="User Avatar"
      width={100}
      height={100}
      priority
    />
  );
}

What will the browser display when this component is rendered?
NextJS
import Image from 'next/image';

export default function Avatar() {
  return (
    <Image
      src="/avatar.png"
      alt="User Avatar"
      width={100}
      height={100}
      priority
    />
  );
}
AAn optimized 100x100 pixel image with lazy loading disabled and alt text 'User Avatar'.
BA 100x100 pixel image that loads lazily with no alt text.
CA broken image icon because the src path is invalid.
DAn unoptimized image with width and height ignored.
Attempts:
2 left
💡 Hint
Remember that the priority prop disables lazy loading and improves loading speed for important images.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses the next/image component to fill its parent container?
You want an image to fill its parent container responsively using next/image. Which code snippet is correct?
A<Image src="/photo.jpg" alt="Photo" fill />
B<Image src="/photo.jpg" alt="Photo" layout="fill" />
C<Image src="/photo.jpg" alt="Photo" width="100%" height="100%" />
D<Image src="/photo.jpg" alt="Photo" style={{width: '100%', height: '100%'}} />
Attempts:
2 left
💡 Hint
Check the latest Next.js syntax for filling containers with images.
🔧 Debug
advanced
2:00remaining
Why does this next/image component cause a build error?
Examine this code:

import Image from 'next/image';

export default function Logo() {
  return (
    <Image
      src="https://example.com/logo.png"
      alt="Company Logo"
      width={200}
      height={100}
    />
  );
}

Why might this cause a build error in Next.js?
NextJS
import Image from 'next/image';

export default function Logo() {
  return (
    <Image
      src="https://example.com/logo.png"
      alt="Company Logo"
      width={200}
      height={100}
    />
  );
}
AThe alt prop is missing and required.
BThe width and height props must be strings, not numbers.
CThe src prop cannot use https URLs, only local paths.
DExternal image domains must be added to next.config.js under images.domains.
Attempts:
2 left
💡 Hint
Think about Next.js security and optimization rules for external images.
❓ state_output
advanced
2:00remaining
What happens to the image size when using next/image with sizes prop?
Given this component:

import Image from 'next/image';

export default function Banner() {
  return (
    <Image
      src="/banner.jpg"
      alt="Banner"
      width={1200}
      height={400}
      sizes="(max-width: 600px) 480px, 800px"
    />
  );
}

What effect does the sizes prop have on the rendered image?
NextJS
import Image from 'next/image';

export default function Banner() {
  return (
    <Image
      src="/banner.jpg"
      alt="Banner"
      width={1200}
      height={400}
      sizes="(max-width: 600px) 480px, 800px"
    />
  );
}
AThe image height changes dynamically but width stays fixed.
BThe browser loads a 480px wide image on screens 600px or smaller, otherwise 800px wide image.
CThe sizes prop causes a syntax error and image won't load.
DThe image always loads at 1200px width ignoring sizes.
Attempts:
2 left
💡 Hint
The sizes prop helps the browser pick the best image size based on viewport width.
🧠 Conceptual
expert
2:00remaining
Which statement best explains how next/image optimizes images?
Select the most accurate explanation of how Next.js next/image optimizes images automatically.
AIt requires manual image resizing and format conversion before use.
BIt only lazy loads images but does not change image size or format.
CIt automatically resizes, compresses, and serves images in modern formats based on device and network conditions.
DIt disables caching to always fetch fresh images from the server.
Attempts:
2 left
💡 Hint
Think about what benefits image optimization provides for user experience.

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